exchange a couple of types with Closeable
[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  * @version $Id$
35  */
36 public class Closer {
37
38         /**
39          * Closes the given closeable.
40          * 
41          * @param closeable
42          *            The closeable to close
43          */
44         public static void close(Closeable closeable) {
45                 if (closeable != null) {
46                         try {
47                                 closeable.close();
48                         } catch (IOException ioe1) {
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                         }
65                 }
66         }
67
68         /**
69          * Closes the given jar file.
70          * 
71          * @param jarFile
72          *            The jar file to close
73          */
74         public static void close(JarFile jarFile) {
75                 if (jarFile != null) {
76                         try {
77                                 jarFile.close();
78                         } catch (IOException e) {
79                         }
80                 }
81         }
82
83         /**
84          * Closes the given zip file.
85          * 
86          * @param zipFile
87          *            The zip file to close
88          */
89         public static void close(ZipFile zipFile) {
90                 if (zipFile != null) {
91                         try {
92                                 zipFile.close();
93                         } catch (IOException e) {
94                         }
95                 }
96         }
97
98 }