Package com.sk89q.worldedit.util.io
Class Closer
java.lang.Object
com.sk89q.worldedit.util.io.Closer
- All Implemented Interfaces:
Closeable
,AutoCloseable
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes allCloseable
instances that have been added to thisCloser
.static Closer
create()
Creates a newCloser
.<C extends Closeable>
Cregister
(C closeable) <Z extends ZipFile>
Zregister
(Z zipFile) Stores the given throwable and rethrows it.<X extends Exception>
RuntimeExceptionStores the given throwable and rethrows it.<X1 extends Exception,
X2 extends Exception>
RuntimeExceptionStores the given throwable and rethrows it.Callrethrow(Throwable)
with the given exception, but before throwing the exception, also close this Closer.
-
Method Details
-
create
Creates a newCloser
. -
register
- Returns:
- the given
closeable
-
register
- Returns:
- the given
closeable
-
rethrowAndClose
Callrethrow(Throwable)
with the given exception, but before throwing the exception, also close this Closer. Exceptions from closing are added tot
as suppressed exceptions.- Parameters:
t
- the throwable that should be re-thrown- Throws:
IOException
- ift
is an IOException, or one occurs
-
rethrow
Stores the given throwable and rethrows it. It will be rethrown as is if it is anIOException
,RuntimeException
orError
. Otherwise, it will be rethrown wrapped in aRuntimeException
. 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 anIOException
,RuntimeException
,Error
or a checked exception of the given type. Otherwise, it will be rethrown wrapped in aRuntimeException
. 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 IOExceptionX
- 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 anIOException
,RuntimeException
,Error
or a checked exception of either of the given types. Otherwise, it will be rethrown wrapped in aRuntimeException
. 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 IOExceptionX1
- when the given throwable is of the declared type X1X2
- when the given throwable is of the declared type X2
-
close
Closes allCloseable
instances that have been added to thisCloser
. If an exception was thrown in the try block and passed to one of theexceptionThrown
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-