Closeables.java

  1. /*******************************************************************************
  2.  * Copyright 2013 André Rouél
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  ******************************************************************************/
  16. package net.sf.uadetector.internal.util;

  17. import java.io.Closeable;
  18. import java.io.IOException;

  19. import javax.annotation.Nullable;

  20. import net.sf.uadetector.exception.CannotCloseException;

  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;

  23. /**
  24.  * This class is intended to provide utility methods to close {@link Closeable} instances.
  25.  *
  26.  * @author André Rouél
  27.  */
  28. public final class Closeables {

  29.     private static final Logger LOG = LoggerFactory.getLogger(Closeables.class);

  30.     /**
  31.      * Closes a {@link Closeable} and swallows an occurring {@link IOException} if argument {@code swallowIOException}
  32.      * is {@code true}, otherwise the {@code IOException} will be thrown.
  33.      * <p>
  34.      * This method does nothing if a null reference is passed as {@code closeable}.
  35.      *
  36.      * @param closeable
  37.      *            the {@code Closeable} object to be closed, or {@code null}
  38.      * @param swallowIOException
  39.      *            {@code true} if an occurring {@code IOException} should be swallowed and logged or {@code false} to
  40.      *            throw it
  41.      *
  42.      * @throws IOException
  43.      *             if the given {@code closeable} cannot be closed
  44.      */
  45.     public static void close(@Nullable final Closeable closeable, final boolean swallowIOException) throws IOException {
  46.         if (closeable != null) {
  47.             try {
  48.                 closeable.close();
  49.             } catch (final IOException e) {
  50.                 if (!swallowIOException) {
  51.                     throw e;
  52.                 }
  53.                 LOG.warn(e.getLocalizedMessage(), e);
  54.             }
  55.         }
  56.     }

  57.     /**
  58.      * Closes a {@link Closeable} and swallows an occurring {@link IOException} if argument {@code swallowIOException}
  59.      * is {@code true}, otherwise the {@code IOException} will be converted into a runtime exception.
  60.      * <p>
  61.      * This method does nothing if a null reference is passed as {@code closeable}.
  62.      *
  63.      * @param closeable
  64.      *            the {@code Closeable} object to be closed, or {@code null}
  65.      * @param swallowIOException
  66.      *            {@code true} if an occurring {@code IOException} should be swallowed and logged or {@code false} to
  67.      *            throw a {@code CannotCloseException}
  68.      *
  69.      * @throws CannotCloseException
  70.      *             if the given {@code closeable} cannot be closed
  71.      */
  72.     public static void closeAndConvert(@Nullable final Closeable closeable, final boolean swallowIOException) {
  73.         if (closeable != null) {
  74.             try {
  75.                 closeable.close();
  76.             } catch (final IOException e) {
  77.                 if (!swallowIOException) {
  78.                     throw new CannotCloseException(e.getLocalizedMessage(), e);
  79.                 }
  80.                 LOG.warn(e.getLocalizedMessage(), e);
  81.             }
  82.         }
  83.     }

  84.     /**
  85.      * <strong>Attention:</strong> This class is not intended to create objects from it.
  86.      */
  87.     private Closeables() {
  88.         // This class is not intended to create objects from it.
  89.     }

  90. }