add closer
[jSite2.git] / src / net / pterodactylus / util / io / Closer.java
1 /*
2  * freenet - Closer.java Copyright © 2007 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 net.pterodactylus.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.util.jar.JarFile;
27 import java.util.zip.ZipFile;
28
29 /**
30  * Closes various resources. The resources are checked for being
31  * <code>null</code> before being closed, and every possible execption is
32  * swallowed. That makes this class perfect for use in the finally blocks of
33  * try-catch-finally blocks.
34  * 
35  * @author David &lsquo;Roden&rsquo; &lt;bombe@freenetproject.org&gt;
36  * @version $Id$
37  */
38 public class Closer {
39
40         /**
41          * Closes the given output stream.
42          * 
43          * @param outputStream
44          *            The output stream to close
45          */
46         public static void close(OutputStream outputStream) {
47                 if (outputStream != null) {
48                         try {
49                                 outputStream.close();
50                         } catch (IOException ioe1) {
51                         }
52                 }
53         }
54
55         /**
56          * Closes the given input stream.
57          * 
58          * @param inputStream
59          *            The input stream to close
60          */
61         public static void close(InputStream inputStream) {
62                 if (inputStream != null) {
63                         try {
64                                 inputStream.close();
65                         } catch (IOException ioe1) {
66                         }
67                 }
68         }
69
70         /**
71          * Closes the given writer.
72          * 
73          * @param writer
74          *            The writer to close
75          */
76         public static void close(Writer writer) {
77                 if (writer != null) {
78                         try {
79                                 writer.close();
80                         } catch (IOException ioe1) {
81                         }
82                 }
83         }
84
85         /**
86          * Closes the given reader.
87          * 
88          * @param reader
89          *            The reader to close
90          */
91         public static void close(Reader reader) {
92                 if (reader != null) {
93                         try {
94                                 reader.close();
95                         } catch (IOException ioe1) {
96                         }
97                 }
98         }
99
100         /**
101          * Closes the given jar file.
102          * 
103          * @param jarFile
104          *            The jar file to close
105          */
106         public static void close(JarFile jarFile) {
107                 if (jarFile != null) {
108                         try {
109                                 jarFile.close();
110                         } catch (IOException e) {
111                         }
112                 }
113         }
114
115         /**
116          * Closes the given zip file.
117          * 
118          * @param zipFile
119          *            The zip file to close
120          */
121         public static void close(ZipFile zipFile) {
122                 if (zipFile != null) {
123                         try {
124                                 zipFile.close();
125                         } catch (IOException e) {
126                         }
127                 }
128         }
129
130 }