Package com.globalmentor.io
Class OutputStreamDecorator<O extends OutputStream>
java.lang.Object
java.io.OutputStream
com.globalmentor.io.OutputStreamDecorator<O>
- Type Parameters:
O
- The type of output stream being decorated.
- All Implemented Interfaces:
Disposable
,Closeable
,Flushable
,AutoCloseable
- Direct Known Subclasses:
CaptureOutputStream
,ProgressOutputStream
,TempOutputStream
public class OutputStreamDecorator<O extends OutputStream>
extends OutputStream
implements Disposable
Wraps an existing output stream.
- Author:
- Garret Wilson
- API Note:
- The decorated output stream is released when this stream is closed.
- Implementation Note:
- This decorator provides convenience methods
beforeClose()
andafterClose()
called before and after the stream is closed, respectively.
-
Constructor Summary
ConstructorsConstructorDescriptionOutputStreamDecorator
(O outputStream) Decorates the given output stream, automatically callingdispose()
when closed.OutputStreamDecorator
(O outputStream, boolean autoDispose) Decorates the given output stream. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Called after the stream is successfully closed.protected void
Called before the stream is closed.protected OutputStream
Checks to make sure the decorated output stream is available.void
close()
void
close
(boolean closeDecoratedStream) Closes this output stream and releases any system resources associated with the stream.void
dispose()
Uninitializes the object.protected void
finalize()
void
flush()
protected O
protected void
setOutputStream
(O outputStream) Changes the decorated output stream.void
write
(byte[] b) void
write
(byte[] b, int off, int len) void
write
(int b)
-
Constructor Details
-
OutputStreamDecorator
Decorates the given output stream, automatically callingdispose()
when closed.- Parameters:
outputStream
- The output stream to decorate.- Throws:
NullPointerException
- if the given stream isnull
.
-
OutputStreamDecorator
Decorates the given output stream.- Parameters:
outputStream
- The output stream to decorate.autoDispose
- Whether the stream should be automatically disposed when closed.- Throws:
NullPointerException
- if the given stream isnull
.
-
-
Method Details
-
getOutputStream
- Returns:
- The output stream being decorated, or
null
if it has been released after this stream was closed.
-
setOutputStream
Changes the decorated output stream.This method can be used by child classes to change the decorated output stream, but cannot be used to remove the output stream---this can be done only by calling
close()
.- Parameters:
outputStream
- The new output stream to decorate.- Throws:
NullPointerException
- if the given output stream isnull
.
-
write
- Specified by:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
checkOutputStream
Checks to make sure the decorated output stream is available.- Returns:
- The decorated output stream.
- Throws:
IOException
- if there is no output stream, indicating that the stream is already closed.
-
beforeClose
Called before the stream is closed.- Throws:
IOException
- if an I/O error occurs.
-
afterClose
Called after the stream is successfully closed.- Throws:
IOException
- if an I/O error occurs.
-
close
Closes this output stream and releases any system resources associated with the stream. A closed stream cannot perform output operations and cannot be reopened. If auto-dispose is enabled,dispose()
will be called if closing is successful.- Parameters:
closeDecoratedStream
- Whether the decorated stream should also be closed.- Throws:
IOException
- if an I/O error occurs.- See Also:
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
dispose
public void dispose()Uninitializes the object. After calling this method, the object should not be used further. This method can be called multiple times without danger. All exceptions should be caught and dealt with in this method. Child classes must call the parent class version.- Specified by:
dispose
in interfaceDisposable
- Implementation Specification:
- This version closes the output stream and releases it, if still available.
-
finalize
-