Java Stream Close closeAll(Collection closeables)

Here you can find the source of closeAll(Collection closeables)

Description

Closes given Collection collection of AutoCloseable closeables .

License

Open Source License

Parameter

Parameter Description
closeables the closeables to close
T the type of closeable

Exception

Parameter Description
IOException an exception

Declaration

public static <T extends AutoCloseable> void closeAll(Collection<T> closeables) throws IOException 

Method Source Code


//package com.java2s;
/*// w  w w . jav a  2  s. c om
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.util.Collection;

public class Main {
    /**
     * Closes given {@link Collection collection} of {@link AutoCloseable closeables}.
     *
     * @param closeables the closeables to close
     * @param <T> the type of closeable
     * @throws IOException
     * @see #closeAll(AutoCloseable[])
     */
    public static <T extends AutoCloseable> void closeAll(Collection<T> closeables) throws IOException {
        closeAll(closeables.toArray(new AutoCloseable[closeables.size()]));
    }

    /**
     * Closes given array of {@link AutoCloseable closeables}. If any {@link AutoCloseable#close()} call throws
     * {@link IOException} than it will be rethrown to the caller after calling {@link AutoCloseable#close()}
     * on other given resources. If more than one {@link AutoCloseable#close()} throw than resulting exception will
     * have suppressed exceptions. See {@link Exception#addSuppressed(Throwable)}
     *
     * @param closeables the closeables to close
     * @param <T> the type of closeable
     * @throws IOException
     */
    @SafeVarargs
    public static <T extends AutoCloseable> void closeAll(T... closeables) throws IOException {
        Exception closeException = null;
        for (T closeable : closeables) {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (Exception e) {
                    if (closeException == null) {
                        closeException = e;
                    } else {
                        closeException.addSuppressed(e);
                    }
                }
            }
        }
        if (closeException != null) {
            throw new IOException("Exception closing multiple resources", closeException);
        }
    }
}

Related

  1. close(ZipFile file)
  2. close(ZipFile file)
  3. close(ZipFile zipFile)
  4. closeAll()
  5. closeAll()
  6. closeAll(Exception firstEx, Closeable... objects)
  7. closeAll(Iterable closeables)
  8. closeAll(T... io)
  9. closeAllStreamsQuietly(final Closeable... closeables)