Example usage for java.io Closeable close

List of usage examples for java.io Closeable close

Introduction

In this page you can find the example usage for java.io Closeable close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this stream and releases any system resources associated with it.

Usage

From source file:Main.java

/**
 * Closes {@code closeable}, ignoring any checked exceptions. Does nothing
 * if {@code closeable} is null./*from  w  w w  . j a  va2 s.  c  o m*/
 */
public static void closeQuietly(Closeable closeable) {
    if (closeable != null) {
        try {
            closeable.close();
        } catch (RuntimeException rethrown) {
            throw rethrown;
        } catch (Exception ignored) {
        }
    }
}

From source file:com.ngdata.sep.util.io.Closer.java

/**
 * Closes anything {@link Closeable}, catches any throwable that might occur during closing and logs it as an error.
 *///from ww w .  j  a  v  a 2  s .c  o m
public static void close(Closeable closeable) {
    if (closeable != null) {
        try {
            closeable.close();
        } catch (Throwable t) {
            Log log = LogFactory.getLog(Closer.class);
            log.error("Error closing object of type " + closeable.getClass().getName(), t);
        }
    }
}

From source file:com.excella.deploy.agent.core.StreamUtil.java

/**
 * Helper method for safely closing an object that implements the Closeable interface.
 * This method is null safe.  /*from  ww w. j ava2 s .c om*/
 *
 * @param item object that should be closed.
 * @param nullify true if the object should be set to null, false if it should merely
 * be closed.
 */
public static final void close(Closeable item, boolean nullify) {
    try {
        if (item != null) {
            item.close();
        }
    } catch (IOException e) {
        log.error("Unable to close the connection");
    } finally {
        if (nullify)
            item = null;
    }
}

From source file:Main.java

/**
 * Simple utility to close {@link Closeable} instance.
 *
 * A typical usage is as follows:/*from  w w w .ja v  a  2  s. co m*/
 * <pre>{@code
 *   Closeable stream = ...;
 *   boolean succeeded = false;
 *   try {
 *     // Read data from stream here.
 *     ...
 *
 *     succeeded = true;
 *   } finally {
 *     close(stream, !succeeded);
 *   }
 * }</pre>
 *
 * @param closeable
 * @param ignoreException
 * @throws IOException
 */
public static void close(Closeable closeable, boolean ignoreException) throws IOException {
    try {
        closeable.close();
    } catch (IOException e) {
        if (!ignoreException) {
            throw e;
        }
    }
}

From source file:org.sejda.util.IOUtils.java

/**
 * Null safe close of the given {@link Closeable}.
 *
 * @param closeable//from   www .ja v  a  2s.c o m
 *            to be closed
 * @throws IOException
 */
public static void close(Closeable closeable) throws IOException {
    if (closeable != null) {
        closeable.close();
    }
}

From source file:Main.java

public static void closeQuietly(Closeable c) {
    if (c != null) {
        try {//  w w  w .  j  av a  2s. c  o m
            c.close();
        } catch (IOException e) {
        }
    }
}

From source file:com.jaeksoft.searchlib.util.IOUtils.java

public static final void close(final Closeable closeable) {
    if (closeable == null)
        return;//from   w ww  . j  a v  a2  s .c  om
    try {
        closeable.close();
    } catch (IOException e) {
        Logging.warn(e);
    }
}

From source file:Main.java

/**
 * Closes a closeable while suppressing any {@code IOException} that occurs.
 * @param closeable the socket to close// www .  j  ava 2 s  . co  m
 */
public static void closeQuietly(Closeable closeable) {
    if (closeable == null)
        return;
    try {
        closeable.close();
    } catch (IOException ex) {
        assert true; // avoid an empty catch
    }
}

From source file:Main.java

public static void closeAll(Closeable a, Closeable b) throws IOException {
    Throwable thrown = null;/* www. ja va2s .c  om*/
    try {
        a.close();
    } catch (Throwable e) {
        thrown = e;
    }
    try {
        b.close();
    } catch (Throwable e) {
        if (thrown == null)
            thrown = e;
    }
    if (thrown == null)
        return;
    if (thrown instanceof IOException)
        throw (IOException) thrown;
    if (thrown instanceof RuntimeException)
        throw (RuntimeException) thrown;
    if (thrown instanceof Error)
        throw (Error) thrown;
    throw new AssertionError(thrown);
}

From source file:Main.java

/**
 * Closes a specified Closeable, suppressing any checked exceptions. This has no effect if the closeable is null.
 *
 * @param closeable the object to close, may be null in which case this does nothing
 */// w  ww  .j a v a 2 s .  c o m
public static void closeSilently(Closeable closeable) {
    if (closeable == null) {
        return; // silently ignore null
    }

    try {
        closeable.close();
    } catch (RuntimeException rethrown) {
        throw rethrown;
    } catch (Exception ignored) {
        // silently ignore checked exceptions
    }
}