Java Stream Close closeIgnoreExceptions(Closeable stream)

Here you can find the source of closeIgnoreExceptions(Closeable stream)

Description

Closes the given stream and ignores all IOExceptions thrown by the close operation.

License

Apache License

Declaration

public static void closeIgnoreExceptions(Closeable stream) 

Method Source Code

//package com.java2s;
/**/*  w w w.  j  av  a 2s . c om*/
 * 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.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;

public class Main {
    /**
     * Closes the given stream and ignores all IOExceptions thrown by the close operation. For streams which buffer
     * data, the close operation may cause the data to be flushed. This may result in an IOException during closing,
     * which actually indicates that some data from a previous write was not fully written. If this is a concern, the
     * stream should be explicitly flushed with OutputStream#flush() before calling this method.
     *
     * The normal use of this function is to ignore errors while closing streams in finally blocks where IOExceptions
     * are not expected:
     *
     * OutputStream out = new SomeStream();
     * try {
     *     out.write(data);
     *     out.flush();
     * } catch (IOException e) {
     *     ExceptionUtil.closeIgnoreExceptions(out);
     * }
     *
     * This method properly avoids swallowing InterruptedIOExceptions received during the close.
     */
    public static void closeIgnoreExceptions(Closeable stream) {
        if (stream == null)
            return;

        try {
            stream.close();
        } catch (InterruptedIOException e) {
            /*
             * We simply re-interrupt the thread so that the interrupt will be handled later by the caller.
             *
             * We could throw a DelphixInterruptedException, but this method will frequently be called in finally blocks
             * where we want to avoid the need for nested finally blocks to make sure all cleanup happens.
             *
             * For example in this case if closing stream1 throws a DelphixInterruptedException stream2 would not be
             * closed:
             *
             * } finally {
             *     ExceptionUtil.closeIgnoreExceptions(stream1);
             *     ExceptionUtil.closeIgnoreExceptions(stream2);
             * }
             */
            Thread.currentThread().interrupt();
        } catch (IOException e) {
            /*
             * Ignore other IOExceptions.
             */
        }
    }
}

Related

  1. closeFileWriter(BufferedWriter bw)
  2. closeFileWriter(FileWriter file)
  3. closeGZipStream(GZIPOutputStream outStream)
  4. closeHTML(String pageTemplate, PrintStream out)
  5. closeIgnoreExceptions(Closeable c)
  6. closeIgnoreIOEx(ByteArrayOutputStream baos)
  7. closeIgnoringException(final Closeable closeable)
  8. closeIgnoringExceptions(Closeable c)
  9. closeIgnoringExceptions(Closeable c)