whitespace fixups
[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.Closeable;
22 import java.io.IOException;
23 import java.net.Socket;
24 import java.util.jar.JarFile;
25 import java.util.zip.ZipFile;
26
27 /**
28  * Closes various resources. The resources are checked for being
29  * <code>null</code> before being closed, and every possible execption is
30  * swallowed. That makes this class perfect for use in the finally blocks of
31  * try-catch-finally blocks.
32  *
33  * @author David &lsquo;Roden&rsquo; &lt;bombe@freenetproject.org&gt;
34  */
35 public class Closer {
36
37         /**
38          * Closes the given closeable.
39          *
40          * @param closeable
41          *            The closeable to close
42          */
43         public static void close(Closeable closeable) {
44                 if (closeable != null) {
45                         try {
46                                 closeable.close();
47                         } catch (IOException ioe1) {
48                                 /* ignore. */
49                         }
50                 }
51         }
52
53         /**
54          * Closes the given socket.
55          *
56          * @param socket
57          *            The socket to close
58          */
59         public static void close(Socket socket) {
60                 if (socket != null) {
61                         try {
62                                 socket.close();
63                         } catch (IOException ioe1) {
64                                 /* ignore. */
65                         }
66                 }
67         }
68
69         /**
70          * Closes the given jar file.
71          *
72          * @param jarFile
73          *            The jar file to close
74          */
75         public static void close(JarFile jarFile) {
76                 if (jarFile != null) {
77                         try {
78                                 jarFile.close();
79                         } catch (IOException e) {
80                                 /* ignore. */
81                         }
82                 }
83         }
84
85         /**
86          * Closes the given zip file.
87          *
88          * @param zipFile
89          *            The zip file to close
90          */
91         public static void close(ZipFile zipFile) {
92                 if (zipFile != null) {
93                         try {
94                                 zipFile.close();
95                         } catch (IOException e) {
96                                 /* ignore. */
97                         }
98                 }
99         }
100
101 }