KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > HsqlByteArrayInputStream


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 import java.io.DataInput JavaDoc;
35 import java.io.EOFException JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38
39 /**
40  * This class is a replacement for both java.io.ByteArrayInputStream
41  * (without synchronization) and java.io.DataInputStream
42  *
43  * @author fredt@users
44  * @version 1.7.2
45  * @since 1.7.2
46  */

47 public class HsqlByteArrayInputStream extends InputStream JavaDoc
48 implements DataInput JavaDoc {
49
50     protected byte[] buf;
51     protected int pos;
52     protected int mark = 0;
53     protected int count;
54
55     public HsqlByteArrayInputStream(byte[] buf) {
56
57         this.buf = buf;
58         this.pos = 0;
59         this.count = buf.length;
60     }
61
62     public HsqlByteArrayInputStream(byte[] buf, int offset, int length) {
63
64         this.buf = buf;
65         this.pos = offset;
66         this.count = Math.min(offset + length, buf.length);
67         this.mark = offset;
68     }
69
70     // methods that implement java.io.DataInput
71
public final void readFully(byte[] b) throws IOException JavaDoc {
72         readFully(b, 0, b.length);
73     }
74
75     public final void readFully(byte[] b, int off,
76                                 int len) throws IOException JavaDoc {
77
78         if (len < 0) {
79             throw new IndexOutOfBoundsException JavaDoc();
80         }
81
82         int n = 0;
83
84         while (n < len) {
85             int count = read(b, off + n, len - n);
86
87             if (count < 0) {
88                 throw new EOFException JavaDoc();
89             }
90
91             n += count;
92         }
93     }
94
95     public final boolean readBoolean() throws IOException JavaDoc {
96
97         int ch = read();
98
99         if (ch < 0) {
100             throw new EOFException JavaDoc();
101         }
102
103         return (ch != 0);
104     }
105
106     public final byte readByte() throws IOException JavaDoc {
107
108         int ch = read();
109
110         if (ch < 0) {
111             throw new EOFException JavaDoc();
112         }
113
114         return (byte) ch;
115     }
116
117     public final int readUnsignedByte() throws IOException JavaDoc {
118
119         int ch = read();
120
121         if (ch < 0) {
122             throw new EOFException JavaDoc();
123         }
124
125         return ch;
126     }
127
128     public short readShort() throws IOException JavaDoc {
129
130         if (count - pos < 2) {
131             pos = count;
132
133             throw new EOFException JavaDoc();
134         }
135
136         int ch1 = buf[pos++] & 0xff;
137         int ch2 = buf[pos++] & 0xff;
138
139         return (short) ((ch1 << 8) + (ch2));
140     }
141
142     public final int readUnsignedShort() throws IOException JavaDoc {
143
144         int ch1 = read();
145         int ch2 = read();
146
147         if ((ch1 | ch2) < 0) {
148             throw new EOFException JavaDoc();
149         }
150
151         return (ch1 << 8) + (ch2);
152     }
153
154     public final char readChar() throws IOException JavaDoc {
155
156         int ch1 = read();
157         int ch2 = read();
158
159         if ((ch1 | ch2) < 0) {
160             throw new EOFException JavaDoc();
161         }
162
163         return (char) ((ch1 << 8) + (ch2));
164     }
165
166     public int readInt() throws IOException JavaDoc {
167
168         if (count - pos < 4) {
169             pos = count;
170
171             throw new EOFException JavaDoc();
172         }
173
174         int ch1 = buf[pos++] & 0xff;
175         int ch2 = buf[pos++] & 0xff;
176         int ch3 = buf[pos++] & 0xff;
177         int ch4 = buf[pos++] & 0xff;
178
179         return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4));
180     }
181
182     public final long readLong() throws IOException JavaDoc {
183         return (((long) readInt()) << 32)
184                + (((long) readInt()) & 0xffffffffL);
185     }
186
187     public final float readFloat() throws IOException JavaDoc {
188         return Float.intBitsToFloat(readInt());
189     }
190
191     public final double readDouble() throws IOException JavaDoc {
192         return Double.longBitsToDouble(readLong());
193     }
194
195     public int skipBytes(int n) throws IOException JavaDoc {
196         return (int) skip(n);
197     }
198
199     public String JavaDoc readLine() throws IOException JavaDoc {
200
201         /** @todo: this will probably be useful */
202         throw new java.lang.RuntimeException JavaDoc("not implemented.");
203     }
204
205     public String JavaDoc readUTF() throws IOException JavaDoc {
206
207         int bytecount = readUnsignedShort();
208
209         if (pos + bytecount >= count) {
210             throw new EOFException JavaDoc();
211         }
212
213         String JavaDoc result = StringConverter.readUTF(buf, pos, bytecount);
214
215         pos += bytecount;
216
217         return result;
218     }
219
220 // methods that extend java.io.InputStream
221
public int read() {
222         return (pos < count) ? (buf[pos++] & 0xff)
223                              : -1;
224     }
225
226     public int read(byte[] b, int off, int len) {
227
228         if (pos >= count) {
229             return -1;
230         }
231
232         if (pos + len > count) {
233             len = count - pos;
234         }
235
236         if (len <= 0) {
237             return 0;
238         }
239
240         System.arraycopy(buf, pos, b, off, len);
241
242         pos += len;
243
244         return len;
245     }
246
247     public long skip(long n) {
248
249         if (pos + n > count) {
250             n = count - pos;
251         }
252
253         if (n < 0) {
254             return 0;
255         }
256
257         pos += n;
258
259         return n;
260     }
261
262     public int available() {
263         return count - pos;
264     }
265
266     public boolean markSupported() {
267         return true;
268     }
269
270     public void mark(int readAheadLimit) {
271         mark = pos;
272     }
273
274     public void reset() {
275         pos = mark;
276     }
277
278     public void close() throws IOException JavaDoc {}
279 }
280
Popular Tags