Class Closer

java.lang.Object
com.sk89q.worldedit.util.io.Closer
All Implemented Interfaces:
Closeable, AutoCloseable

public final class Closer extends Object implements Closeable
  • Method Details

    • create

      public static Closer create()
      Creates a new Closer.
    • register

      public <C extends Closeable> C register(C closeable)
      Registers the given closeable to be closed when this Closer is closed.
      Returns:
      the given closeable
    • register

      public <Z extends ZipFile> Z register(Z zipFile)
      Registers the given zipFile to be closed when this Closer is closed.
      Returns:
      the given closeable
    • rethrowAndClose

      public RuntimeException rethrowAndClose(Throwable t) throws IOException
      Call rethrow(Throwable) with the given exception, but before throwing the exception, also close this Closer. Exceptions from closing are added to t as suppressed exceptions.
      Parameters:
      t - the throwable that should be re-thrown
      Throws:
      IOException - if t is an IOException, or one occurs
    • rethrow

      public RuntimeException rethrow(Throwable e) throws IOException
      Stores the given throwable and rethrows it. It will be rethrown as is if it is an IOException, RuntimeException or Error. Otherwise, it will be rethrown wrapped in a RuntimeException. Note: Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type.

      This method always throws, and as such should be called as throw closer.rethrow(e); to ensure the compiler knows that it will throw.

      Returns:
      this method does not return; it always throws
      Throws:
      IOException - when the given throwable is an IOException
    • rethrow

      public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, X
      Stores the given throwable and rethrows it. It will be rethrown as is if it is an IOException, RuntimeException, Error or a checked exception of the given type. Otherwise, it will be rethrown wrapped in a RuntimeException. Note: Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type.

      This method always throws, and as such should be called as throw closer.rethrow(e, ...); to ensure the compiler knows that it will throw.

      Returns:
      this method does not return; it always throws
      Throws:
      IOException - when the given throwable is an IOException
      X - when the given throwable is of the declared type X
    • rethrow

      public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow(Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1, X2
      Stores the given throwable and rethrows it. It will be rethrown as is if it is an IOException, RuntimeException, Error or a checked exception of either of the given types. Otherwise, it will be rethrown wrapped in a RuntimeException. Note: Be sure to declare all of the checked exception types your try block can throw when calling an overload of this method so as to avoid losing the original exception type.

      This method always throws, and as such should be called as throw closer.rethrow(e, ...); to ensure the compiler knows that it will throw.

      Returns:
      this method does not return; it always throws
      Throws:
      IOException - when the given throwable is an IOException
      X1 - when the given throwable is of the declared type X1
      X2 - when the given throwable is of the declared type X2
    • close

      public void close() throws IOException
      Closes all Closeable instances that have been added to this Closer. If an exception was thrown in the try block and passed to one of the exceptionThrown methods, any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the first exception to be thrown from an attempt to close a closeable will be thrown and any additional exceptions that are thrown after that will be suppressed.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException