Java Stream Close close(Closeable... streams)

Here you can find the source of close(Closeable... streams)

Description

Closes the specified streams, suppressing any IOExceptions for inputstreams and readers.

License

Open Source License

Parameter

Parameter Description
streams the streams to close

Exception

Parameter Description

Declaration

public static void close(Closeable... streams) throws IOException 

Method Source Code

//package com.java2s;

import java.io.Closeable;

import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;

import java.io.Reader;

public class Main {
    /**/*ww  w.  jav  a2  s .  c o  m*/
     * Closes the specified streams, suppressing any IOExceptions for inputstreams and readers.
     * Even if an I/O exception is thrown, all streams can be considered closed.
     * @param streams the streams to close
     * @throws java.io.IOException if an i/o exception occurs while closing any outputstream or writer
     */
    public static void close(Closeable... streams) throws IOException {
        close(streams, 0);
    }

    private static void close(Closeable[] streams, int idx) throws IOException {
        if (idx >= streams.length) {
            return; // done
        }
        Closeable stream = streams[idx];
        try {
            if (stream != null) {
                if (stream instanceof Flushable) {
                    ((Flushable) stream).flush();
                }
                stream.close();
            }
        } catch (IOException ex) {
            if (!(stream instanceof InputStream || stream instanceof Reader)) {
                throw ex; // ignore io exceptions for input streams and readers
            }
        } finally {
            close(streams, idx + 1);
        }
    }
}

Related

  1. close(Closeable resource)
  2. close(Closeable resource)
  3. close(Closeable resource)
  4. close(Closeable resource, File file)
  5. close(Closeable stream)
  6. close(Collection inCloseables)
  7. close(DataOutputStream out)
  8. close(File file)
  9. close(FileReader fr)