Java Stream Close closeQuietly(InputStream inputstream)

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

Description

Closes an input stream quietly (i.e.

License

LGPL

Declaration

public static void closeQuietly(InputStream inputstream) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

public class Main {
    /**//  w  w  w .j  ava  2  s.co  m
     * Closes an input stream quietly (i.e. no exception is thrown) while
     * inspecting its class. The inspection is done to avoid closing streams
     * associates with jar files that may cause later errors like
     * 
     * <pre>
     * Caused by: java.io.IOException: Stream closed
     * </pre>
     * 
     * So, all streams whose class name does not start with
     * <code>sun.net.www.protocol.jar.JarURLConnection</code> will be closed
     * 
     * @since 2016-09-20
     */
    public static void closeQuietly(InputStream inputstream) {
        try {
            close(inputstream);
        } catch (IOException e) {
            // do nothing as we should be quiet
        }
    }

    /**
     * Closes an input stream while inspecting its class. The inspection is done
     * to avoid closing streams associates with jar files that may cause later
     * errors like
     * 
     * <pre>
     * Caused by: java.io.IOException: Stream closed
     * </pre>
     * 
     * So, all streams whose class name does not start with
     * <code>sun.net.www.protocol.jar.JarURLConnection</code> will be closed
     * 
     * @throws IOException
     * 
     * @since 2016-09-20
     */
    public static void close(InputStream inputstream) throws IOException {
        if (inputstream != null) {
            String inputstreamClassName = inputstream.getClass().getName();
            if (!inputstreamClassName.startsWith("sun.net.www.protocol.jar.JarURLConnection")) {
                inputstream.close();
            }
        }
    }
}

Related

  1. closeQuietly(InputStream input)
  2. closeQuietly(InputStream input)
  3. closeQuietly(InputStream input)
  4. closeQuietly(InputStream input)
  5. closeQuietly(InputStream input)
  6. closeQuietly(InputStream is)
  7. closeQuietly(InputStream is)
  8. closeQuietly(InputStream is)
  9. closeQuietly(InputStream is)