001: package com.quadcap.sql.io;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedReader;
042: import java.io.DataInput;
043: import java.io.EOFException;
044: import java.io.InputStream;
045: import java.io.IOException;
046:
047: import com.quadcap.io.InputStreamReader;
048:
049: import com.quadcap.util.Debug;
050: import com.quadcap.util.Util;
051:
052: /**
053: * Implement low level serialization
054: */
055: public class DataInputStream extends InputStream implements DataInput {
056: InputStream in;
057: static final boolean buffered = false;
058:
059: static final int MAX = 4096;
060: byte[] buf = new byte[MAX];
061: int pos;
062: int len;
063:
064: public DataInputStream(InputStream in) {
065: this .in = in;
066: this .pos = 0;
067: this .len = 0;
068: }
069:
070: long position;
071:
072: public void setPosition(long p) {
073: this .position = p;
074: }
075:
076: public long getPosition() {
077: return position;
078: }
079:
080: public void setInputStream(InputStream in) {
081: this .in = in;
082: }
083:
084: public InputStream getInputStream() {
085: return in;
086: }
087:
088: public void readFully(byte[] b) throws IOException {
089: read(b, 0, b.length);
090: }
091:
092: public void readFully(byte[] b, int off, int len)
093: throws IOException {
094: read(b, off, len);
095: }
096:
097: public int skipBytes(int n) throws IOException {
098: return (int) skip(n);
099: }
100:
101: public boolean readBoolean() throws IOException {
102: return read() == 1;
103: }
104:
105: public byte readByte() throws IOException {
106: return (byte) read();
107: }
108:
109: public int readUnsignedByte() throws IOException {
110: return read();
111: }
112:
113: public char readChar() throws IOException {
114: return (char) readUnsignedShort();
115: }
116:
117: final long readLongByte() throws IOException {
118: return readUnsignedByte();
119: }
120:
121: //#ifdef SMALLDB
122: public long readLong() throws IOException {
123: long ret = 0;
124: long b;
125: int shift = 0;
126: do {
127: b = read();
128: ret |= ((b & 0x7f) << shift);
129: shift += 7;
130: } while ((b & 0x80) != 0);
131: return ret;
132: }
133:
134: public int readInt() throws IOException {
135: int ret = 0;
136: int b;
137: int shift = 0;
138: do {
139: b = read();
140: ret |= ((b & 0x7f) << shift);
141: shift += 7;
142: } while ((b & 0x80) != 0);
143: return ret;
144: }
145:
146: public short readShort() throws IOException {
147: return (short) readInt();
148: }
149:
150: public int readUnsignedShort() throws IOException {
151: return readInt();
152: }
153:
154: //#else
155: //- public short readShort() throws IOException {
156: //- return (short)readUnsignedShort();
157: //- }
158: //-
159: //- public int readUnsignedShort() throws IOException {
160: //- int ret = readUnsignedByte() << 8;
161: //- ret += readUnsignedByte();
162: //- return ret;
163: //- }
164: //-
165: //- public int readInt() throws IOException {
166: //- int ret = readUnsignedByte() << 24;
167: //- ret |= (readUnsignedByte() << 16);
168: //- ret |= (readUnsignedByte() << 8);
169: //- ret |= readUnsignedByte();
170: //- return ret;
171: //- }
172: //-
173: //- public long readLong() throws IOException {
174: //- long ret = readLongByte() << 56;
175: //- ret |= (readLongByte() << 48);
176: //- ret |= (readLongByte() << 40);
177: //- ret |= (readLongByte() << 32);
178: //- ret |= (readLongByte() << 24);
179: //- ret |= (readLongByte() << 16);
180: //- ret |= (readLongByte() << 8);
181: //- ret |= readLongByte();
182: //- return ret;
183: //- }
184: //#endif
185:
186: public float readFloat() throws IOException {
187: return Float.intBitsToFloat(readInt());
188: }
189:
190: public double readDouble() throws IOException {
191: return Double.longBitsToDouble(readLong());
192: }
193:
194: public String readLine() throws IOException {
195: InputStreamReader ns = new InputStreamReader(this );
196: BufferedReader r = new BufferedReader(ns);
197: return r.readLine();
198: }
199:
200: public String readUTF() throws IOException {
201: throw new IOException("readUTF not implemented");
202: }
203:
204: final boolean fillBuffer() throws IOException {
205: boolean ret = pos >= len;
206: if (ret) {
207: len = in.read(buf, 0, buf.length);
208: pos = 0;
209: ret = pos >= len;
210: }
211: return ret;
212: }
213:
214: public int read() throws IOException {
215: int ret = -1;
216: if (buffered) {
217: if (!fillBuffer()) {
218: ret = buf[pos++] & 0xff;
219: position++;
220: }
221: } else {
222: ret = in.read();
223: if (ret < 0) {
224: throw new EOFException();
225: }
226: position++;
227: }
228: //#ifdef DEBUG
229: if (trace)
230: Debug.println("DIS.read() = " + ret);
231: //#endif
232: return ret;
233: }
234:
235: public int read(byte[] b, int off, int cnt) throws IOException {
236: int ret = 0;
237: //#ifdef DEBUG
238: int xoff = off;
239: int xcnt = cnt;
240: //#endif
241: if (buffered) {
242: while (cnt > 0 && !fillBuffer()) {
243: int amt = len - pos;
244: if (amt > cnt)
245: amt = cnt;
246: System.arraycopy(buf, pos, b, off, amt);
247: cnt -= amt;
248: off += amt;
249: ret += amt;
250: pos += amt;
251: position += amt;
252: }
253: } else {
254: while (cnt > 0) {
255: int amt = in.read(b, off, cnt);
256: ret += amt;
257: position += amt;
258: off += amt;
259: cnt -= amt;
260: }
261: }
262: //#ifdef DEBUG
263: if (trace) {
264: Debug.println("DIS.read() = " + ret + ": "
265: + Util.hexBytes(b, xoff, ret));
266: }
267: //#endif
268: return ret;
269: }
270:
271: //#ifdef DEBUG
272: static final boolean trace = false;
273:
274: //#endif
275:
276: public int read(byte[] buf) throws IOException {
277: return read(buf, 0, buf.length);
278: }
279:
280: public long skip(long n) throws IOException {
281: long ret = 0;
282: if (buffered) {
283: while (n > 0 && !fillBuffer()) {
284: long amt = len - pos;
285: if (amt > n)
286: amt = n;
287: n -= amt;
288: pos += amt;
289: position += amt;
290: }
291: } else {
292: ret = in.skip(n);
293: }
294: return ret;
295: }
296: }
|