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