View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.io.Closeable;
17  import java.io.IOException;
18  import java.net.ServerSocket;
19  import java.net.Socket;
20  
21  /**
22   * Static utility method for {@link Closeable} objects.
23   *
24   * @author Carl Harris
25   */
26  public class CloseUtil {
27  
28      /**
29       * Closes a closeable while suppressing any {@code IOException} that occurs.
30       * @param closeable the socket to close
31       */
32      public static void closeQuietly(Closeable closeable) {
33          if (closeable == null)
34              return;
35          try {
36              closeable.close();
37          } catch (IOException ex) {
38              assert true; // avoid an empty catch
39          }
40      }
41  
42      /**
43       * Closes a socket while suppressing any {@code IOException} that occurs.
44       * @param socket the socket to close
45       */
46      public static void closeQuietly(Socket socket) {
47          if (socket == null)
48              return;
49          try {
50              socket.close();
51          } catch (IOException ex) {
52              assert true; // avoid an empty catch
53          }
54      }
55  
56      /**
57       * Closes a server socket while suppressing any {@code IOException} that 
58       * occurs.
59       * @param serverSocket the socket to close
60       */
61      public static void closeQuietly(ServerSocket serverSocket) {
62          if (serverSocket == null)
63              return;
64          try {
65              serverSocket.close();
66          } catch (IOException ex) {
67              assert true; // avoid an empty catch
68          }
69      }
70  
71  }