Java Stream Close closeStream(final InputStream stream)

Here you can find the source of closeStream(final InputStream stream)

Description

Closes the InputStream, catching the exception and returning false on failure.

License

Open Source License

Parameter

Parameter Description
stream InputStream to close; a null value will cause false to be returned

Return

true if successful, false if null or otherwise.

Declaration

public static boolean closeStream(final InputStream stream) 

Method Source Code

//package com.java2s;
/*/*from w  w w  .j  ava2s.  c  om*/
 *  Copyright (C) 2011-2014 Brian Groenke
 *  All rights reserved.
 * 
 *  This file is part of the 2DX Graphics Library.
 *
 *  This Source Code Form is subject to the terms of the
 *  Mozilla Public License, v. 2.0. If a copy of the MPL 
 *  was not distributed with this file, You can obtain one at 
 *  http://mozilla.org/MPL/2.0/.
 */

import java.io.*;

public class Main {
    /**
     * Closes the InputStream, catching the exception and returning false on
     * failure. In the case that stream is null, this method will quietly return
     * false.
     * 
     * @param stream
     *            InputStream to close; a null value will cause false to be
     *            returned
     * @return true if successful, false if null or otherwise.
     */
    public static boolean closeStream(final InputStream stream) {

        if (stream == null) {
            return false;
        }
        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * Closes the OutputStream, catching the exception and returning false on
     * failure. In the case that stream is null, this method will quietly return
     * false.
     * 
     * @param stream
     *            OutputStream to close; a null value will cause false to be
     *            returned
     * @return true if successful, false if null or otherwise.
     */
    public static boolean closeStream(final OutputStream stream) {

        if (stream == null) {
            return false;
        }
        try {
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}

Related

  1. closeStream(Closeable... closeable)
  2. closeStream(Closeable... streams)
  3. closeStream(FileInputStream in)
  4. closeStream(final Closeable stream)
  5. closeStream(final InputStream in)
  6. closeStream(final java.io.Closeable stream)
  7. closeStream(final Object stream)
  8. closeStream(InputStream in)
  9. closeStream(InputStream in)