01: package de.regnis.q.sequence.line;
02:
03: import java.io.*;
04:
05: import de.regnis.q.sequence.line.simplifier.*;
06:
07: /**
08: * @author Marc Strapetz
09: */
10: final class QSequenceLineReader {
11:
12: // Fields =================================================================
13:
14: private byte[] buffer;
15:
16: // Setup ==================================================================
17:
18: public QSequenceLineReader() {
19: this (8192);
20: }
21:
22: public QSequenceLineReader(int initialBufferSize) {
23: buffer = new byte[initialBufferSize];
24: }
25:
26: // Static =================================================================
27:
28: public void read(InputStream rawStream, QSequenceLineCache cache,
29: QSequenceLineSimplifier simplifier) throws IOException {
30: final BufferedInputStream stream = new BufferedInputStream(
31: rawStream);
32: try {
33: int pushBack = -1;
34: int from = 0;
35: int length = 0;
36: for (;;) {
37: int ch = pushBack;
38: if (ch != -1) {
39: pushBack = -1;
40: } else {
41: ch = stream.read();
42: }
43:
44: if (ch != -1) {
45: append(length, (byte) (ch & 0xff));
46: length++;
47: }
48:
49: switch (ch) {
50: case '\r':
51: pushBack = stream.read();
52: if (pushBack == '\n') {
53: append(length, (byte) (pushBack & 0xff));
54: length++;
55: pushBack = -1;
56: }
57: case '\n':
58: case -1:
59: if (length > 0) {
60: final byte[] bytes;
61: bytes = new byte[length];
62: System.arraycopy(buffer, 0, bytes, 0, length);
63: cache.addLine(new QSequenceLine(from, bytes,
64: simplifier));
65: }
66: from = from + length;
67: length = 0;
68: }
69:
70: if (ch == -1) {
71: break;
72: }
73: }
74: } finally {
75: stream.close();
76: }
77: }
78:
79: // Utils ==================================================================
80:
81: private void append(int position, byte ch) {
82: if (position >= buffer.length) {
83: final byte[] newArray = new byte[buffer.length * 2];
84: System.arraycopy(buffer, 0, newArray, 0, buffer.length);
85: buffer = newArray;
86: }
87: buffer[position] = ch;
88: }
89: }
|