try to close all streams as soon as possible to prevent leaks (inserting large sites...
[jSite.git] / src / de / todesbaum / util / io / Closer.java
1 /*
2  * todesbaum-lib - Copyright (C) 2006 David Roden
3  * 
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  * 
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.util.io;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.Reader;
25 import java.io.Writer;
26 import java.net.ServerSocket;
27 import java.net.Socket;
28 import java.sql.Connection;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.Statement;
32
33 /**
34  * Helper class that can close all kinds of resources without throwing exception
35  * so that clean-up code can be written with less code. All methods check that
36  * the given resource is not <code>null</code> before invoking the close()
37  * method of the respective type.
38  * 
39  * @author <a href="mailto:bombe@freenetproject.org">David &lsquo;Bombe&squo;
40  *         Roden</a>
41  * @version $Id$
42  */
43 public class Closer {
44
45         /**
46          * Closes the given result set.
47          * 
48          * @param resultSet
49          *            The result set to close
50          * @see ResultSet#close()
51          */
52         public static void close(ResultSet resultSet) {
53                 if (resultSet != null) {
54                         try {
55                                 resultSet.close();
56                         } catch (SQLException ioe1) {
57                         }
58                 }
59         }
60
61         /**
62          * Closes the given statement.
63          * 
64          * @param statement
65          *            The statement to close
66          * @see Statement#close()
67          */
68         public static void close(Statement statement) {
69                 if (statement != null) {
70                         try {
71                                 statement.close();
72                         } catch (SQLException ioe1) {
73                         }
74                 }
75         }
76
77         /**
78          * Closes the given connection.
79          * 
80          * @param connection
81          *            The connection to close
82          * @see Connection#close()
83          */
84         public static void close(Connection connection) {
85                 if (connection != null) {
86                         try {
87                                 connection.close();
88                         } catch (SQLException ioe1) {
89                         }
90                 }
91         }
92
93         /**
94          * Closes the given server socket.
95          * 
96          * @param serverSocket
97          *            The server socket to close
98          * @see ServerSocket#close()
99          */
100         public static void close(ServerSocket serverSocket) {
101                 if (serverSocket != null) {
102                         try {
103                                 serverSocket.close();
104                         } catch (IOException ioe1) {
105                         }
106                 }
107         }
108
109         /**
110          * Closes the given socket.
111          * 
112          * @param socket
113          *            The socket to close
114          * @see Socket#close()
115          */
116         public static void close(Socket socket) {
117                 if (socket != null) {
118                         try {
119                                 socket.close();
120                         } catch (IOException ioe1) {
121                         }
122                 }
123         }
124
125         /**
126          * Closes the given input stream.
127          * 
128          * @param inputStream
129          *            The input stream to close
130          * @see InputStream#close()
131          */
132         public static void close(InputStream inputStream) {
133                 if (inputStream != null) {
134                         try {
135                                 inputStream.close();
136                         } catch (IOException ioe1) {
137                         }
138                 }
139         }
140
141         /**
142          * Closes the given output stream.
143          * 
144          * @param outputStream
145          *            The output stream to close
146          * @see OutputStream#close()
147          */
148         public static void close(OutputStream outputStream) {
149                 if (outputStream != null) {
150                         try {
151                                 outputStream.close();
152                         } catch (IOException ioe1) {
153                         }
154                 }
155         }
156
157         /**
158          * Closes the given reader.
159          * 
160          * @param reader
161          *            The reader to close
162          * @see Reader#close()
163          */
164         public static void close(Reader reader) {
165                 if (reader != null) {
166                         try {
167                                 reader.close();
168                         } catch (IOException ioe1) {
169                         }
170                 }
171         }
172
173         /**
174          * Closes the given writer.
175          * 
176          * @param writer
177          *            The write to close
178          * @see Writer#close()
179          */
180         public static void close(Writer writer) {
181                 if (writer != null) {
182                         try {
183                                 writer.close();
184                         } catch (IOException ioe1) {
185                         }
186                 }
187         }
188
189 }