Java Stream Close close(final InputStream is)

Here you can find the source of close(final InputStream is)

Description

Close the input stream quietly consuming any exceptions.

License

Open Source License

Parameter

Parameter Description
is the input stream to close

Declaration

public static void close(final InputStream is) 

Method Source Code

//package com.java2s;
/*//from   w  w w.jav a  2 s . c om
 * Copyright (c) 2006 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial API and implementation
 */

import java.io.Closeable;

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

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class Main {
    /**
     * Close the Reader quietly consuming any exceptions.
     * 
     * @param reader the Reader to close
     */
    public static void close(final Reader reader) {
        close((Closeable) reader);
    }

    /**
     * Close the Writer quietly consuming any exceptions.
     * 
     * @param writer the writer to close
     */
    public static void close(final Writer writer) {
        close((Closeable) writer);
    }

    /**
     * Close the input stream quietly consuming any exceptions.
     * 
     * @param is the input stream to close
     */
    public static void close(final InputStream is) {
        close((Closeable) is);
    }

    /**
     * Close the output stream quietly consuming any exceptions.
     * 
     * @param os the output stream to close
     */
    public static void close(final OutputStream os) {
        close((Closeable) os);
    }

    /**
     * Close the given group of closable objects quietly consuming 
     * any exceptions.
     * 
     * @param closeables the closable objects to close
     */
    public static void close(final Closeable... closeables) {
        if (closeables == null) {
            return;
        }
        for (final Closeable closeable : closeables) {
            close(closeable);
        }
    }

    /**
     * Closes a {@code Closeable} object quietly consuming any exceptions thrown. 
     * @param closeable the object to close, may be null or already closed
     */
    public static void close(final Closeable closeable) {
        try {
            if (closeable != null)
                closeable.close();
        } catch (final IOException ignore) {
        }
    }
}

Related

  1. close(FileReader fr)
  2. close(FileReader reader, BufferedReader br)
  3. close(final Closeable resource, final String resourceId)
  4. close(final Exception e, final Closeable... objects)
  5. close(final InputStream anInputStream)
  6. close(final InputStream stream)
  7. close(final InputStream stream)
  8. close(final Logger logger, final Closeable... closeables)
  9. close(final Object stream)