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       * 
31       * @param closeable the socket to close
32       */
33      public static void closeQuietly(Closeable closeable) {
34          if (closeable == null)
35              return;
36          try {
37              closeable.close();
38          } catch (IOException ex) {
39              assert true; // avoid an empty catch
40          }
41      }
42  
43      /**
44       * Closes a socket while suppressing any {@code IOException} that occurs.
45       * 
46       * @param socket the socket to close
47       */
48      public static void closeQuietly(Socket socket) {
49          if (socket == null)
50              return;
51          try {
52              socket.close();
53          } catch (IOException ex) {
54              assert true; // avoid an empty catch
55          }
56      }
57  
58      /**
59       * Closes a server socket while suppressing any {@code IOException} that occurs.
60       * 
61       * @param serverSocket the socket to close
62       */
63      public static void closeQuietly(ServerSocket serverSocket) {
64          if (serverSocket == null)
65              return;
66          try {
67              serverSocket.close();
68          } catch (IOException ex) {
69              assert true; // avoid an empty catch
70          }
71      }
72  
73  }