Class Closeables

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public final class Closeables
    extends java.lang.Object
    implements java.io.Closeable
    A set of Closeables that can be closed at once.
    Author:
    Christian Kohlschütter
    • Constructor Summary

      Constructors 
      Constructor Description
      Closeables()  
      Closeables​(java.io.Closeable... closeable)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(java.io.Closeable closeable)
      Adds the given closeable.
      boolean add​(java.lang.ref.WeakReference<java.io.Closeable> closeable)
      Adds the given closeable, but only using a weak reference.
      void close()  
      void close​(java.io.IOException superException)
      Closes all registered closeables.
      boolean remove​(java.io.Closeable closeable)
      Removes the given closeable.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Closeables

        public Closeables()
      • Closeables

        public Closeables​(java.io.Closeable... closeable)
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • close

        public void close​(java.io.IOException superException)
                   throws java.io.IOException
        Closes all registered closeables.
        Parameters:
        superException - If set, any exceptions thrown in here will be chained to the given exception via addSuppressed, and then thrown.
        Throws:
        java.io.IOException - if an exception occurs.
      • add

        public boolean add​(java.lang.ref.WeakReference<java.io.Closeable> closeable)
        Adds the given closeable, but only using a weak reference.
        Parameters:
        closeable - The closeable.
        Returns:
        true iff the closeable was added, false if it was null or already added before.
      • add

        public boolean add​(java.io.Closeable closeable)
        Adds the given closeable.
        Parameters:
        closeable - The closeable.
        Returns:
        true iff the closeable was added, false if it was null or already added before.
      • remove

        public boolean remove​(java.io.Closeable closeable)
        Removes the given closeable.
        Parameters:
        closeable - The closeable.
        Returns:
        true iff the closeable was removed, fale if it was null or not previously added.