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