Package com.globalmentor.io
Class OutputStreamDecorator<O extends java.io.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
,java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
- Direct Known Subclasses:
CaptureOutputStream
,ProgressOutputStream
,TempOutputStream
public class OutputStreamDecorator<O extends java.io.OutputStream> extends java.io.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
Constructors Constructor Description OutputStreamDecorator(O outputStream)
Decorates the given output stream, automatically callingdispose()
when closed.OutputStreamDecorator(O outputStream, boolean autoDispose)
Decorates the given output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
afterClose()
Called after the stream is successfully closed.protected void
beforeClose()
Called before the stream is closed.protected java.io.OutputStream
checkOutputStream()
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
getOutputStream()
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 Detail
-
OutputStreamDecorator
public OutputStreamDecorator(O outputStream)
Decorates the given output stream, automatically callingdispose()
when closed.- Parameters:
outputStream
- The output stream to decorate.- Throws:
java.lang.NullPointerException
- if the given stream isnull
.
-
OutputStreamDecorator
public OutputStreamDecorator(O outputStream, boolean autoDispose)
Decorates the given output stream.- Parameters:
outputStream
- The output stream to decorate.autoDispose
- Whether the stream should be automatically disposed when closed.- Throws:
java.lang.NullPointerException
- if the given stream isnull
.
-
-
Method Detail
-
getOutputStream
protected O getOutputStream()
- Returns:
- The output stream being decorated, or
null
if it has been released after this stream was closed.
-
setOutputStream
protected void setOutputStream(O outputStream)
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:
java.lang.NullPointerException
- if the given output stream isnull
.
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
checkOutputStream
protected java.io.OutputStream checkOutputStream() throws java.io.IOException
Checks to make sure the decorated output stream is available.- Returns:
- The decorated output stream.
- Throws:
java.io.IOException
- if there is no output stream, indicating that the stream is already closed.
-
beforeClose
protected void beforeClose() throws java.io.IOException
Called before the stream is closed.- Throws:
java.io.IOException
- if an I/O error occurs.
-
afterClose
protected void afterClose() throws java.io.IOException
Called after the stream is successfully closed.- Throws:
java.io.IOException
- if an I/O error occurs.
-
close
public void close(boolean closeDecoratedStream) throws java.io.IOException
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:
java.io.IOException
- if an I/O error occurs.- See Also:
beforeClose()
,afterClose()
,dispose()
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.OutputStream
- Throws:
java.io.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
protected void finalize() throws java.lang.Throwable
- Overrides:
finalize
in classjava.lang.Object
- Implementation Specification:
- This version calls
dispose()
. - Throws:
java.lang.Throwable
-
-