01: package uk.org.ponder.streamutil;
02:
03: import java.io.OutputStream;
04: import java.io.FilterOutputStream;
05: import java.io.IOException;
06:
07: import uk.org.ponder.util.Logger;
08:
09: /** A TrackerOutputStream wraps an OutputStream and guarantees to call a specified
10: * callback immediately after the stream it wraps is closed.
11: */
12:
13: public class TrackerOutputStream extends FilterOutputStream {
14: private StreamClosedCallback callback;
15:
16: /** Constructs a TrackerOutputStream wrapping the specified OutputStream,
17: * to call the supplied callback on closure.
18: * @param is The OutputStream to be wrapped.
19: * @param callback The callback to be after the output stream is closed.
20: */
21: public TrackerOutputStream(OutputStream is,
22: StreamClosedCallback callback) {
23: super (is);
24: this .callback = callback;
25: }
26:
27: public void close() throws IOException {
28: try {
29: super .close();
30: Logger.println("TrackerOutputStream closed",
31: Logger.DEBUG_INFORMATIONAL);
32: } finally {
33: try {
34: if (callback != null)
35: callback.streamClosed(out);
36: } finally {
37: callback = null;
38: }
39: }
40: }
41:
42: /** Returns the underlying output stream which has been wrapped.
43: * @return the underlying output stream which has been wrapped.
44: */
45:
46: public OutputStream getUnderlyingStream() {
47: return out;
48: }
49: }
|