01: package uk.org.ponder.streamutil;
02:
03: import java.io.OutputStream;
04: import java.io.IOException;
05:
06: public class RandomAccessOutputStream extends OutputStream {
07: private RandomAccessWrite internalfile;
08:
09: public RandomAccessOutputStream(RandomAccessWrite internalfile)
10: throws IOException {
11: System.out
12: .println("RandomAccessOutputStream created at offset "
13: + internalfile.getFilePointer());
14: this .internalfile = internalfile;
15: }
16:
17: private StreamClosedCallback callback;
18:
19: public void setClosingCallback(StreamClosedCallback callback) {
20: this .callback = callback;
21: }
22:
23: // public RandomAccessFile getRandomAccessFile() {
24: // return internalfile;
25: // }
26:
27: public void write(byte[] array, int start, int length)
28: throws IOException {
29: System.out.println("Write performed of length " + length
30: + " at offset " + internalfile.getFilePointer());
31: internalfile.write(array, start, length);
32: }
33:
34: public void write(int towrite) throws IOException {
35: internalfile.write(towrite);
36: }
37:
38: /** Close this stream object. Note that this does NOT close the underlying
39: * random access file.
40: */
41: public void close() throws IOException {
42: System.out.println("RandomAccessOutputStream closed");
43: // new Throwable().printStackTrace();
44: if (callback != null) {
45: callback.streamClosed(this);
46: callback = null;
47: }
48: internalfile = null;
49: }
50: }
|