Java Stream Close closeQuietly(final InputStream inputStream)

Here you can find the source of closeQuietly(final InputStream inputStream)

Description

Closes the InputStream if it is not null, swallowing any exceptions.

License

Apache License

Parameter

Parameter Description
inputStream the InputStream to close

Declaration

public static void closeQuietly(final InputStream inputStream) 

Method Source Code

//package com.java2s;
/*//from   w ww  .  j a  v a 2 s  .  com
 * Copyright (C) 2014 the original author or authors.
 *
 * 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 InputStream if it is not null, swallowing any exceptions.
     *
     * @param inputStream the InputStream to close
     */
    public static void closeQuietly(final InputStream inputStream) {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                // do nothing, close quietly
            }
        }
    }

    /**
     * Closes the OutputStream if it is not null, swallowing any exceptions.
     *
     * @param outputStream the OutputStream to close
     */
    public static void closeQuietly(final OutputStream outputStream) {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                // do nothing, close quietly
            }
        }
    }

    /**
     * Closes the Reader if it is not null, swallowing any exceptions.
     *
     * @param reader the Reader to close
     */
    public static void closeQuietly(final Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                // do nothing, close quietly
            }
        }
    }

    /**
     * Closes the Writer if it is not null, swallowing any exceptions.
     *
     * @param writer the Writer to close
     */
    public static void closeQuietly(final Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // do nothing, close quietly
            }
        }
    }
}

Related

  1. closeQuietly(BufferedReader br)
  2. closeQuietly(Closeable input, Logger log)
  3. closeQuietly(Closeable stream)
  4. closeQuietly(Closeable stream)
  5. closeQuietly(final InputStream in)
  6. closeQuietly(final Reader reader)
  7. closeQuietly(final ZipFile... zipFiles)
  8. closeQuietly(ImageInputStream ins)
  9. closeQuietly(ImageReader iReader)