001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.lib;
032:
033: import java.io.DataInput;
034: import java.io.EOFException;
035: import java.io.IOException;
036: import java.io.InputStream;
037:
038: /**
039: * This class is a replacement for both java.io.ByteArrayInputStream
040: * (without synchronization) and java.io.DataInputStream
041: *
042: * @author fredt@users
043: * @version 1.7.2
044: * @since 1.7.2
045: */
046: public class HsqlByteArrayInputStream extends InputStream implements
047: DataInput {
048:
049: protected byte[] buf;
050: protected int pos;
051: protected int mark = 0;
052: protected int count;
053:
054: public HsqlByteArrayInputStream(byte[] buf) {
055:
056: this .buf = buf;
057: this .pos = 0;
058: this .count = buf.length;
059: }
060:
061: public HsqlByteArrayInputStream(byte[] buf, int offset, int length) {
062:
063: this .buf = buf;
064: this .pos = offset;
065: this .count = Math.min(offset + length, buf.length);
066: this .mark = offset;
067: }
068:
069: // methods that implement java.io.DataInput
070: public final void readFully(byte[] b) throws IOException {
071: readFully(b, 0, b.length);
072: }
073:
074: public final void readFully(byte[] b, int off, int len)
075: throws IOException {
076:
077: if (len < 0) {
078: throw new IndexOutOfBoundsException();
079: }
080:
081: int n = 0;
082:
083: while (n < len) {
084: int count = read(b, off + n, len - n);
085:
086: if (count < 0) {
087: throw new EOFException();
088: }
089:
090: n += count;
091: }
092: }
093:
094: public final boolean readBoolean() throws IOException {
095:
096: int ch = read();
097:
098: if (ch < 0) {
099: throw new EOFException();
100: }
101:
102: return (ch != 0);
103: }
104:
105: public final byte readByte() throws IOException {
106:
107: int ch = read();
108:
109: if (ch < 0) {
110: throw new EOFException();
111: }
112:
113: return (byte) ch;
114: }
115:
116: public final int readUnsignedByte() throws IOException {
117:
118: int ch = read();
119:
120: if (ch < 0) {
121: throw new EOFException();
122: }
123:
124: return ch;
125: }
126:
127: public short readShort() throws IOException {
128:
129: if (count - pos < 2) {
130: pos = count;
131:
132: throw new EOFException();
133: }
134:
135: int ch1 = buf[pos++] & 0xff;
136: int ch2 = buf[pos++] & 0xff;
137:
138: return (short) ((ch1 << 8) + (ch2));
139: }
140:
141: public final int readUnsignedShort() throws IOException {
142:
143: int ch1 = read();
144: int ch2 = read();
145:
146: if ((ch1 | ch2) < 0) {
147: throw new EOFException();
148: }
149:
150: return (ch1 << 8) + (ch2);
151: }
152:
153: public final char readChar() throws IOException {
154:
155: int ch1 = read();
156: int ch2 = read();
157:
158: if ((ch1 | ch2) < 0) {
159: throw new EOFException();
160: }
161:
162: return (char) ((ch1 << 8) + (ch2));
163: }
164:
165: public int readInt() throws IOException {
166:
167: if (count - pos < 4) {
168: pos = count;
169:
170: throw new EOFException();
171: }
172:
173: int ch1 = buf[pos++] & 0xff;
174: int ch2 = buf[pos++] & 0xff;
175: int ch3 = buf[pos++] & 0xff;
176: int ch4 = buf[pos++] & 0xff;
177:
178: return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4));
179: }
180:
181: public final long readLong() throws IOException {
182: return (((long) readInt()) << 32)
183: + (((long) readInt()) & 0xffffffffL);
184: }
185:
186: public final float readFloat() throws IOException {
187: return Float.intBitsToFloat(readInt());
188: }
189:
190: public final double readDouble() throws IOException {
191: return Double.longBitsToDouble(readLong());
192: }
193:
194: public int skipBytes(int n) throws IOException {
195: return (int) skip(n);
196: }
197:
198: public String readLine() throws IOException {
199:
200: /** @todo: this will probably be useful */
201: throw new java.lang.RuntimeException("not implemented.");
202: }
203:
204: public String readUTF() throws IOException {
205:
206: int bytecount = readUnsignedShort();
207:
208: if (pos + bytecount >= count) {
209: throw new EOFException();
210: }
211:
212: String result = StringConverter.readUTF(buf, pos, bytecount);
213:
214: pos += bytecount;
215:
216: return result;
217: }
218:
219: // methods that extend java.io.InputStream
220: public int read() {
221: return (pos < count) ? (buf[pos++] & 0xff) : -1;
222: }
223:
224: public int read(byte[] b, int off, int len) {
225:
226: if (pos >= count) {
227: return -1;
228: }
229:
230: if (pos + len > count) {
231: len = count - pos;
232: }
233:
234: if (len <= 0) {
235: return 0;
236: }
237:
238: System.arraycopy(buf, pos, b, off, len);
239:
240: pos += len;
241:
242: return len;
243: }
244:
245: public long skip(long n) {
246:
247: if (pos + n > count) {
248: n = count - pos;
249: }
250:
251: if (n < 0) {
252: return 0;
253: }
254:
255: pos += n;
256:
257: return n;
258: }
259:
260: public int available() {
261: return count - pos;
262: }
263:
264: public boolean markSupported() {
265: return true;
266: }
267:
268: public void mark(int readAheadLimit) {
269: mark = pos;
270: }
271:
272: public void reset() {
273: pos = mark;
274: }
275:
276: public void close() throws IOException {
277: }
278: }
|