1194N/A/*
1194N/A * CDDL HEADER START
1194N/A *
1194N/A * The contents of this file are subject to the terms of the
1194N/A * Common Development and Distribution License (the "License").
1194N/A * You may not use this file except in compliance with the License.
1194N/A *
1194N/A * See LICENSE.txt included in this distribution for the specific
1194N/A * language governing permissions and limitations under the License.
1194N/A *
1194N/A * When distributing Covered Code, include this CDDL HEADER in each
1194N/A * file and include the License file at LICENSE.txt.
1194N/A * If applicable, add the following below this CDDL HEADER, with the
1194N/A * fields enclosed by brackets "[]" replaced with your own identifying
1194N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1194N/A *
1194N/A * CDDL HEADER END
1194N/A */
1220N/A
1194N/A/*
1194N/A * Copyright (c) 2011 Trond Norbye
1239N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
1194N/A */
1194N/Apackage org.opensolaris.opengrok.util;
1194N/A
1239N/Aimport java.io.Closeable;
1194N/Aimport java.io.IOException;
1195N/Aimport java.net.ServerSocket;
1195N/Aimport java.net.Socket;
1194N/Aimport java.util.logging.Level;
1194N/Aimport java.util.logging.Logger;
1194N/A
1194N/A/**
1194N/A * A small utility class to provide common functionality related to
1194N/A * IO so that we don't need to duplicate the logic all over the place.
1194N/A *
1194N/A * @author Trond Norbye <trond.norbye@gmail.com>
1194N/A */
1194N/Apublic final class IOUtils {
1194N/A
1194N/A private static final Logger log = Logger.getLogger(IOUtils.class.getName());
1194N/A
1194N/A private IOUtils() {
1194N/A // singleton
1194N/A }
1194N/A
1327N/A /**
1327N/A * Try to close the given object if not {@code null} and log errors if an
1327N/A * exception occures.
1327N/A * @param c object to close.
1327N/A */
1327N/A public static final void close(Closeable c) {
1239N/A if (c != null) {
1194N/A try {
1239N/A c.close();
1194N/A } catch (IOException e) {
1327N/A log.warning("Failed to close resource: " + e.getMessage());
1327N/A log.log(Level.FINE, "close", e);
1461N/A } catch (NullPointerException e) {
1461N/A // Lucene's Analyzer tends to throw it sometimes, but not always
1461N/A if (log.isLoggable(Level.FINE)) {
1461N/A log.log(Level.FINE, "NullPointerException when closing "
1461N/A + c.getClass().getName(), e);
1461N/A }
1194N/A }
1194N/A }
1194N/A }
1194N/A
1327N/A /**
1327N/A * Try to close the given socket if not {@code null} and log errors if an
1327N/A * exception occures.
1327N/A * @param sock socket to close.
1327N/A */
1327N/A public static final void close(ServerSocket sock) {
1195N/A if (sock != null) {
1195N/A try {
1195N/A sock.close();
1195N/A } catch (IOException e) {
1327N/A log.warning("Failed to close socket: " + e.getMessage());
1327N/A log.log(Level.FINE, "close", e);
1195N/A }
1195N/A }
1195N/A }
1195N/A
1327N/A /**
1327N/A * Try to close the given socket if not {@code null} and log errors if an
1327N/A * exception occures.
1327N/A * @param sock socket to close.
1327N/A */
1327N/A public static final void close(Socket sock) {
1195N/A if (sock != null) {
1195N/A try {
1195N/A sock.close();
1195N/A } catch (IOException e) {
1327N/A log.warning("Failed to close socket: " + e.getMessage());
1327N/A log.log(Level.FINE, "close", e);
1195N/A }
1195N/A }
1195N/A }
1194N/A}