001: /*
002: * $Id: GenericInputStream.java,v 1.5 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.java.io;
011:
012: import java.io.InputStream;
013: import java.io.IOException;
014:
015: /**
016: * GenericInputStream
017: *
018: * @author: Jani Lehtimäki
019: */
020: public final class GenericInputStream extends InputStream {
021:
022: private static int BUFFERSIZE = 512;
023:
024: private InputStream _input;
025: private byte[] _array = null;
026: private int _pos = 0;
027: private int _end = 0;
028: private int _line = 1;
029: private int _column = 1;
030:
031: public GenericInputStream(byte[] array) {
032: _input = null;
033: _array = array;
034: _end = array.length;
035: }
036:
037: public GenericInputStream(InputStream input) {
038: _input = input;
039: _array = new byte[BUFFERSIZE];
040: }
041:
042: public GenericInputStream(InputStream input, int bufferSize) {
043: _input = input;
044: _array = new byte[bufferSize];
045: }
046:
047: private final void fillBuffer() throws IOException {
048: if (_input == null) {
049: _end = -1;
050: return;
051: }
052: _pos = 0;
053: try {
054: _end = _input.read(_array);
055: } catch (IOException e) {
056: _end = -1;
057: e.fillInStackTrace();
058: throw e;
059: }
060: }
061:
062: public final synchronized int available() throws IOException {
063: if (_input == null) {
064: return _end - _pos;
065: } else {
066: if (_end == -1) {
067: return 0;
068: }
069: return (_end - _pos) + _input.available();
070: }
071: }
072:
073: public final synchronized int read(byte[] target, int offset,
074: int length) throws IOException {
075: if (_end == -1) {
076: return 0;
077: }
078: int read = 0;
079: int size = (_end - _pos);
080: if (size > 0) {
081: if (length <= size) {
082: System.arraycopy(_array, _pos, target, offset, length);
083: read += length;
084: _pos += length;
085: length = 0;
086: } else {
087: System.arraycopy(_array, _pos, target, offset, size);
088: offset += size;
089: length -= size;
090: read += size;
091: _pos = _end;
092: }
093: }
094: if (length > 0) {
095: if (_input != null) {
096: read += _input.read(target, offset, length);
097: }
098: }
099:
100: int column = _column;
101: int line = _line;
102: for (int i = 0; i < read; i++) {
103: if (target[i] == '\n') {
104: line++;
105: column = 1;
106: } else {
107: column++;
108: }
109: }
110: _line = line;
111: _column = column;
112:
113: return read;
114: }
115:
116: public final synchronized int read() throws IOException {
117: if (_end == -1) {
118: return -1;
119: }
120: if (_pos >= _end) {
121: fillBuffer();
122: }
123: if (_pos < _end) {
124: int ch = _array[_pos++];
125: ch = (ch + 0x100) & 0xff;
126: if (ch == '\n') {
127: _line++;
128: _column = 1;
129: } else {
130: _column++;
131: }
132: return ch;
133: } else {
134: return -1;
135: }
136: }
137:
138: public final synchronized String readLine() throws IOException {
139: byte[] array = _array;
140: StringBuffer buffer = new StringBuffer(64);
141: char ch = 0;
142:
143: int line = _line;
144: int column = _column;
145: while (_end != -1) {
146: if (_pos >= _end) {
147: fillBuffer();
148: continue;
149: }
150: ch = (char) array[_pos++];
151: if (ch == '\n') {
152: line++;
153: column = 1;
154: break;
155: } else {
156: column++;
157: }
158: buffer.append(ch);
159: }
160: _line = line;
161: _column = column;
162:
163: int n = buffer.length();
164: if (n == 0) {
165: if (ch == 0) {
166: return null;
167: } else {
168: return "";
169: }
170: } else {
171: if (buffer.charAt(n - 1) == '\r') {
172: buffer.setLength(n - 1);
173: }
174: return buffer.toString();
175: }
176: }
177:
178: public final synchronized String read(int amount)
179: throws IOException {
180: if (amount <= 0) {
181: return "";
182: }
183: byte[] array = _array;
184: StringBuffer buffer = new StringBuffer(amount);
185: char ch;
186:
187: int line = _line;
188: int column = _column;
189: while ((_end != -1) && (amount > 0)) {
190: if (_pos >= _end) {
191: fillBuffer();
192: continue;
193: }
194: ch = (char) array[_pos++];
195: if (ch == '\n') {
196: line++;
197: column = 1;
198: } else {
199: column++;
200: }
201: buffer.append(ch);
202: amount--;
203: }
204: _line = line;
205: _column = column;
206:
207: if (buffer.length() == 0) {
208: return null;
209: } else {
210: return buffer.toString();
211: }
212: }
213:
214: public final synchronized long skip(long amount) throws IOException {
215: byte[] array = _array;
216: long skipped = 0;
217: int line = _line;
218: int column = _column;
219: while ((_end != -1) && (skipped < amount)) {
220: if (_pos >= _end) {
221: fillBuffer();
222: continue;
223: }
224: if (array[_pos++] == '\n') {
225: line++;
226: column = 1;
227: } else {
228: column++;
229: }
230: skipped++;
231: }
232: _line = line;
233: _column = column;
234: if (_end == -1 && skipped == 0) {
235: return -1;
236: } else {
237: return skipped;
238: }
239: }
240:
241: public final int getColumnNumber() {
242: return _column;
243: }
244:
245: public final int getLineNumber() {
246: return _line;
247: }
248:
249: public final synchronized void close() throws IOException {
250: if (_input != null) {
251: _input.close();
252: }
253: }
254:
255: }
|