Java InputStream Close close(InputStream in)

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

Description

Closes the given InputStream.

License

Apache License

Parameter

Parameter Description
in The InputStream to close.

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Declaration

public static void close(InputStream in) throws IOException 

Method Source Code

//package com.java2s;
/**// w  ww.j  a va2  s  .c  o  m
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class Main {
    /**
     * Closes the given InputStream.
     *
     * @param in
     *            The InputStream to close.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void close(InputStream in) throws IOException {
        try {
            if (in != null) {
                in.close();
                in = null;
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }

        }
    }

    /**
     * Closes the given OutputStream.
     *
     * @param out
     *            The OutputStream to close.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void close(OutputStream out) throws IOException {
        try {
            if (out != null) {
                out.flush();
                out.close();
                out = null;
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
        }
    }

    /**
     * Closes the given Reader.
     *
     * @param reader
     *            The Reader to close.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void close(Reader reader) throws IOException {

        try {
            if (reader != null) {
                reader.close();
                reader = null;
            }
        } catch (final IOException e) {
            throw e;
        } finally {

            if (reader != null) {
                reader.close();
            }

        }
    }

    /**
     * Closes the given Writer.
     *
     * @param writer
     *            The Writer to close.
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void close(Writer writer) throws IOException {
        try {
            if (writer != null) {
                writer.flush();
                writer.close();
                writer = null;
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }
}

Related

  1. close(InputStream in)
  2. close(InputStream in)
  3. close(InputStream in)
  4. close(InputStream in)
  5. close(InputStream in)
  6. close(InputStream in, boolean silent)
  7. close(InputStream in, OutputStream out)
  8. close(InputStream input)
  9. close(InputStream inputStream)