Java Stream Close closeQuietly(InputStream in)

Here you can find the source of closeQuietly(InputStream in)

Description

Method that will close an InputStream ignoring it if it is null and ignoring exceptions.

License

Open Source License

Parameter

Parameter Description
in the InputStream to close.

Declaration

public static void closeQuietly(InputStream in) 

Method Source Code

//package com.java2s;
/**//w ww .  j  a va2s .c  o m
 * Copyright 2011 The ARIES Consortium (http://www.ariesonline.org) and
 * www.integratedmodelling.org. 
    
   This file is part of Thinklab.
    
   Thinklab 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.
    
   Thinklab 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 Thinklab.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Method that will close an {@link InputStream} ignoring it if it is null and ignoring exceptions.
     *
     * @param in the InputStream to close.
     * @since jEdit 4.3pre3
     */
    public static void closeQuietly(InputStream in) {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }

    /**
     * Method that will close an {@link OutputStream} ignoring it if it is null and ignoring exceptions.
     *
     * @param out the OutputStream to close.
     * @since jEdit 4.3pre3
     */
    public static void closeQuietly(OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }

    /**
     * Closes InputStream and/or OutputStream.
     * It makes sure that both streams tried to be closed,
     * even first throws an exception.
     *
     * @throw IOException if stream (is not null and) cannot be closed.
     *
     */
    protected static void close(InputStream iStream, OutputStream oStream) throws IOException {
        try {
            if (iStream != null) {
                iStream.close();
            }
        } finally {
            if (oStream != null) {
                oStream.close();
            }
        }
    }
}

Related

  1. closeQuietly(final Reader reader)
  2. closeQuietly(final ZipFile... zipFiles)
  3. closeQuietly(ImageInputStream ins)
  4. closeQuietly(ImageReader iReader)
  5. closeQuietly(InputStream c)
  6. closeQuietly(InputStream in)
  7. closeQuietly(InputStream input)
  8. closeQuietly(InputStream input)
  9. closeQuietly(InputStream input)