Java Stream Close closeWhileHandlingException(Closeable... objects)

Here you can find the source of closeWhileHandlingException(Closeable... objects)

Description

Closes all given Closeables, suppressing all thrown exceptions.

License

Apache License

Parameter

Parameter Description
objects objects to call <tt>close()</tt> on

Declaration

public static void closeWhileHandlingException(Closeable... objects) 

Method Source Code

//package com.java2s;
/*/*from ww w  .  j  av  a 2  s  . c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.Closeable;
import java.io.IOException;

import java.util.Arrays;

public class Main {
    /**
     * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
     * Some of the <tt>Closeable</tt>s may be null, they are ignored.
     * 
     * @param objects
     *          objects to call <tt>close()</tt> on
     */
    public static void closeWhileHandlingException(Closeable... objects) {
        closeWhileHandlingException(Arrays.asList(objects));
    }

    /**
     * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
     * @see #closeWhileHandlingException(Closeable...)
     */
    public static void closeWhileHandlingException(Iterable<? extends Closeable> objects) {
        for (Closeable object : objects) {
            try {
                if (object != null) {
                    object.close();
                }
            } catch (Throwable t) {
            }
        }
    }

    /**
     * Closes all given <tt>Closeable</tt>s.  Some of the
     * <tt>Closeable</tt>s may be null; they are
     * ignored.  After everything is closed, the method either
     * throws the first exception it hit while closing, or
     * completes normally if there were no exceptions.
     * 
     * @param objects
     *          objects to call <tt>close()</tt> on
     */
    public static void close(Closeable... objects) throws IOException {
        close(Arrays.asList(objects));
    }

    /**
     * Closes all given <tt>Closeable</tt>s.
     * @see #close(Closeable...)
     */
    public static void close(Iterable<? extends Closeable> objects) throws IOException {
        Throwable th = null;

        for (Closeable object : objects) {
            try {
                if (object != null) {
                    object.close();
                }
            } catch (Throwable t) {
                addSuppressed(th, t);
                if (th == null) {
                    th = t;
                }
            }
        }

        reThrow(th);
    }

    /** adds a Throwable to the list of suppressed Exceptions of the first Throwable
     * @param exception this exception should get the suppressed one added
     * @param suppressed the suppressed exception
     */
    private static void addSuppressed(Throwable exception, Throwable suppressed) {
        if (exception != null && suppressed != null) {
            exception.addSuppressed(suppressed);
        }
    }

    /**
     * Simple utility method that takes a previously caught
     * {@code Throwable} and rethrows either {@code
     * IOException} or an unchecked exception.  If the
     * argument is null then this method does nothing.
     */
    public static void reThrow(Throwable th) throws IOException {
        if (th != null) {
            if (th instanceof IOException) {
                throw (IOException) th;
            }
            reThrowUnchecked(th);
        }
    }

    /**
     * Simple utility method that takes a previously caught
     * {@code Throwable} and rethrows it as an unchecked exception.
     * If the argument is null then this method does nothing.
     */
    public static void reThrowUnchecked(Throwable th) {
        if (th != null) {
            if (th instanceof RuntimeException) {
                throw (RuntimeException) th;
            }
            if (th instanceof Error) {
                throw (Error) th;
            }
            throw new RuntimeException(th);
        }
    }
}

Related

  1. closeStreams(InputStream iStream, OutputStream oStream)
  2. closeStreams(InputStream src, OutputStream dest)
  3. closeStreams(Process process)
  4. closeUnchecked(InputStream stream)
  5. closeWarnOnError(Closeable stream, Logger log, String message)
  6. closeWithoutError(Object close)
  7. closeWithRuntimeException(Closeable c)
  8. closeWriteFile(FileOutputStream fileoutputstream)
  9. closeWriter(final Writer writer)