001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import java.io.Closeable;
017import java.io.IOException;
018import java.net.ServerSocket;
019import java.net.Socket;
020
021/**
022 * Static utility method for {@link Closeable} objects.
023 *
024 * @author Carl Harris
025 */
026public class CloseUtil {
027
028    /**
029     * Closes a closeable while suppressing any {@code IOException} that occurs.
030     * 
031     * @param closeable the socket to close
032     */
033    public static void closeQuietly(Closeable closeable) {
034        if (closeable == null)
035            return;
036        try {
037            closeable.close();
038        } catch (IOException ex) {
039            assert true; // avoid an empty catch
040        }
041    }
042
043    /**
044     * Closes a socket while suppressing any {@code IOException} that occurs.
045     * 
046     * @param socket the socket to close
047     */
048    public static void closeQuietly(Socket socket) {
049        if (socket == null)
050            return;
051        try {
052            socket.close();
053        } catch (IOException ex) {
054            assert true; // avoid an empty catch
055        }
056    }
057
058    /**
059     * Closes a server socket while suppressing any {@code IOException} that occurs.
060     * 
061     * @param serverSocket the socket to close
062     */
063    public static void closeQuietly(ServerSocket serverSocket) {
064        if (serverSocket == null)
065            return;
066        try {
067            serverSocket.close();
068        } catch (IOException ex) {
069            assert true; // avoid an empty catch
070        }
071    }
072
073}