Java Stream Close closeQuietly(Object... o)

Here you can find the source of closeQuietly(Object... o)

Description

Quietly close all specified input streams, output streams, readers, and writers.

License

Apache License

Parameter

Parameter Description
o The list of all objects to quietly close.

Declaration

public static void closeQuietly(Object... o) 

Method Source Code


//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.io.*;

public class Main {
    /**/*from  ww  w.  j  av a2s  . c  o m*/
     * Close input stream and ignore any exceptions.
     * No-op if input stream is <jk>null</jk>.
     *
     * @param is The input stream to close.
     */
    public static void closeQuietly(InputStream is) {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        }
    }

    /**
     * Close output stream and ignore any exceptions.
     * No-op if output stream is <jk>null</jk>.
     *
     * @param os The output stream to close.
     */
    public static void closeQuietly(OutputStream os) {
        try {
            if (os != null)
                os.close();
        } catch (IOException e) {
        }
    }

    /**
     * Close reader and ignore any exceptions.
     * No-op if reader is <jk>null</jk>.
     *
     * @param r The reader to close.
     */
    public static void closeQuietly(Reader r) {
        try {
            if (r != null)
                r.close();
        } catch (IOException e) {
        }
    }

    /**
     * Close writer and ignore any exceptions.
     * No-op if writer is <jk>null</jk>.
     *
     * @param w The writer to close.
     */
    public static void closeQuietly(Writer w) {
        try {
            if (w != null)
                w.close();
        } catch (IOException e) {
        }
    }

    /**
     * Quietly close all specified input streams, output streams, readers, and writers.
     *
     * @param o The list of all objects to quietly close.
     */
    public static void closeQuietly(Object... o) {
        for (Object o2 : o) {
            if (o2 instanceof InputStream)
                closeQuietly((InputStream) o2);
            if (o2 instanceof OutputStream)
                closeQuietly((OutputStream) o2);
            if (o2 instanceof Reader)
                closeQuietly((Reader) o2);
            if (o2 instanceof Writer)
                closeQuietly((Writer) o2);
        }
    }
}

Related

  1. closeQuietly(java.io.Closeable writer)
  2. closeQuietly(Object input)
  3. closeQuietly(Object obj)
  4. closeQuietly(Object object)
  5. closeQuietly(Object object)
  6. closeQuietly(ObjectInput objectInput)
  7. closeQuietly(ObjectInput... objectInputs)
  8. closeQuietly(Reader input)
  9. closeQuietly(Reader r)