01: package uk.org.ponder.streamutil;
02:
03: import java.io.RandomAccessFile;
04: import java.io.File;
05: import java.io.IOException;
06:
07: /** This class wraps a <code>java.io.RandomAccessFile</code> object so that it
08: * conforms to the <code>RandomAccessRead</code> interface.
09: */
10:
11: public class RandomAccessReadWrapper implements RandomAccessRead {
12: private StreamClosedCallback callback;
13: protected RandomAccessFile internalraf;
14:
15: protected RandomAccessReadWrapper() {
16: }
17:
18: /** Constructs a RandomAccessReadWrapper wrapping the supplied RandomAccessFile.
19: * @param internalraf The RandomAccessFile to be wrapped.
20: */
21: public RandomAccessReadWrapper(RandomAccessFile internalraf) {
22: this .internalraf = internalraf;
23: }
24:
25: /** Constructs a RandomAccessFile from the supplied File object, and then
26: * wraps it with a RandomAccessReadWrapper.
27: * @param file A file to be opened as a RandomAccessFile and wrapped.
28: */
29: public RandomAccessReadWrapper(File file) throws IOException {
30: internalraf = new RandomAccessFile(file, "r");
31: }
32:
33: /** Constructs a RandomAccessFile for the specified filename, and then
34: * wraps it with a RandomAccessReadWrapper.
35: * @param filename A file to be opened as a RandomAccessFile and wrapped.
36: */
37: public RandomAccessReadWrapper(String filename) throws IOException {
38: internalraf = new RandomAccessFile(filename, "r");
39: }
40:
41: public long length() throws IOException {
42: return internalraf.length();
43: }
44:
45: public void close() throws IOException {
46: try {
47: internalraf.close();
48: } finally {
49: if (callback != null)
50: callback.streamClosed(internalraf);
51: }
52: }
53:
54: public int read() throws IOException {
55: return internalraf.read();
56: }
57:
58: public int read(byte b[], int off, int len) throws IOException {
59: return internalraf.read(b, off, len);
60: }
61:
62: public void seek(long pos) throws IOException {
63: internalraf.seek(pos);
64: }
65:
66: public long getFilePointer() throws IOException {
67: return internalraf.getFilePointer();
68: }
69:
70: public void setStreamClosedCallback(StreamClosedCallback callback) {
71: this.callback = callback;
72: }
73:
74: }
|