01: package de.regnis.q.sequence.line;
02:
03: import java.io.*;
04:
05: /**
06: * @author Marc Strapetz
07: */
08: final class QSequenceLineRAFileDataStream extends InputStream {
09:
10: // Fields =================================================================
11:
12: private final RandomAccessFile myFile;
13: private long offset;
14: private int length;
15:
16: // Setup ==================================================================
17:
18: public QSequenceLineRAFileDataStream(RandomAccessFile myFile,
19: long offset, int length) {
20: this .myFile = myFile;
21: this .length = length;
22: this .offset = offset;
23: }
24:
25: // Implemented ============================================================
26:
27: public int read() throws IOException {
28: byte[] buffer = new byte[] { -1 };
29: read(buffer, 0, 1);
30: return buffer[0];
31: }
32:
33: public int read(byte[] buffer, int userOffset, int userLength)
34: throws IOException {
35: if (length <= 0) {
36: return -1;
37: }
38: int available = (int) (myFile.length() - offset);
39: int toRead = Math.min(available, length);
40: toRead = Math.min(userLength, toRead);
41: length -= toRead;
42:
43: long pos = myFile.length();
44: myFile.seek(offset);
45: int result = myFile.read(buffer, userOffset, toRead);
46: myFile.seek(pos);
47: offset += toRead;
48: return result;
49: }
50:
51: // Accessing ==============================================================
52:
53: public void reset(long offset, int length) {
54: this.offset = offset;
55: this.length = length;
56: }
57: }
|