Java Stream Close closeQuietly(InputStream x)

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

Description

Unconditionally close an InputStream.

License

Open Source License

Parameter

Parameter Description
x the InputStream to close, may be null or already closed

Declaration

public static void closeQuietly(InputStream x) 

Method Source Code


//package com.java2s;
/*//  w  w  w  .j  av  a 2s .c om
 * sulky-modules - several general-purpose modules.
 * Copyright (C) 2007-2011 Joern Huxhorn
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

public class Main {
    /**
     * Unconditionally close an <code>InputStream</code>.
     * <p>
     * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
     * {@link InterruptedIOException} is handled correctly by {@link #interruptIfNecessary(Throwable)}.
     * This is typically used in finally blocks.
     *
     * @param x  the InputStream to close, may be null or already closed
     */
    public static void closeQuietly(InputStream x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (IOException e) {
            interruptIfNecessary(e);
        }
    }

    /**
     * Unconditionally close an <code>Reader</code>.
     * <p>
     * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
     * {@link InterruptedIOException} is handled correctly by {@link #interruptIfNecessary(Throwable)}.
     * This is typically used in finally blocks.
     *
     * @param x  the Reader to close, may be null or already closed
     */
    public static void closeQuietly(Reader x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (IOException e) {
            interruptIfNecessary(e);
        }
    }

    /**
     * Unconditionally close an <code>OutputStream</code>.
     * <p>
     * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
     * {@link InterruptedIOException} is handled correctly by {@link #interruptIfNecessary(Throwable)}.
     * This is typically used in finally blocks.
     *
     * @param x  the OutputStream to close, may be null or already closed
     */
    public static void closeQuietly(OutputStream x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (IOException e) {
            interruptIfNecessary(e);
        }
    }

    /**
     * Unconditionally close a <code>Writer</code>.
     * <p>
     * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
     * {@link InterruptedIOException} is handled correctly by {@link #interruptIfNecessary(Throwable)}.
     * This is typically used in finally blocks.
     *
     * @param x  the Writer to close, may be null or already closed
     */
    public static void closeQuietly(Writer x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (IOException e) {
            interruptIfNecessary(e);
        }
    }

    /**
     * Unconditionally close a <code>RandomAccessFile</code>.
     * <p>
     * Equivalent to {@link RandomAccessFile#close()}, except any exceptions will be ignored.
     * {@link InterruptedIOException} is handled correctly by {@link #interruptIfNecessary(Throwable)}.
     * This is typically used in finally blocks.
     *
     * @param x  the RandomAccessFile to close, may be null or already closed
     */
    public static void closeQuietly(RandomAccessFile x) {
        if (x == null) {
            return;
        }
        try {
            x.close();
        } catch (IOException e) {
            interruptIfNecessary(e);
        }
    }

    /**
     * This method calls Thread.currentThread().interrupt() if any exception
     * in the hierarchy (including all getCause()) is either InterruptedIOException
     * or InterruptedException.
     *
     * This method should be called in every catch(IOException), catch(Exception) or
     * catch(Throwable) block.
     *
     * @param t the Throwable to be checked for interruption. Does nothing if null.
     */
    public static void interruptIfNecessary(Throwable t) {
        if (t == null) {
            return;
        }
        Throwable current = t;
        do {
            if (current instanceof InterruptedIOException || current instanceof InterruptedException) {
                Thread.currentThread().interrupt();
                break;
            }
            current = current.getCause();
        } while (current != null);
    }
}

Related

  1. closeQuietly(InputStream is)
  2. closeQuietly(InputStream is)
  3. closeQuietly(InputStream is)
  4. closeQuietly(InputStream is)
  5. closeQuietly(InputStream stream)
  6. closeQuietly(java.io.Closeable writer)
  7. closeQuietly(Object input)
  8. closeQuietly(Object obj)
  9. closeQuietly(Object object)