KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > io > DataInputStream


1 package com.quadcap.sql.io;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedReader JavaDoc;
42 import java.io.DataInput JavaDoc;
43 import java.io.EOFException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 import com.quadcap.io.InputStreamReader;
48
49 import com.quadcap.util.Debug;
50 import com.quadcap.util.Util;
51
52 /**
53  * Implement low level serialization
54  */

55 public class DataInputStream extends InputStream JavaDoc implements DataInput JavaDoc {
56     InputStream JavaDoc in;
57     static final boolean buffered = false;
58     
59     static final int MAX = 4096;
60     byte[] buf = new byte[MAX];
61     int pos;
62     int len;
63
64     public DataInputStream(InputStream JavaDoc in) {
65         this.in = in;
66         this.pos = 0;
67         this.len = 0;
68     }
69
70     long position;
71     public void setPosition(long p) { this.position = p; }
72     public long getPosition() { return position; }
73
74     public void setInputStream(InputStream JavaDoc in) {
75         this.in = in;
76     }
77     public InputStream JavaDoc getInputStream() {
78         return in;
79     }
80     
81     public void readFully(byte[] b) throws IOException JavaDoc {
82         read(b, 0, b.length);
83     }
84     
85     public void readFully(byte[] b, int off, int len) throws IOException JavaDoc {
86         read(b, off, len);
87     }
88     
89     public int skipBytes(int n) throws IOException JavaDoc {
90         return (int)skip(n);
91     }
92     
93     public boolean readBoolean() throws IOException JavaDoc {
94         return read() == 1;
95     }
96     
97     public byte readByte() throws IOException JavaDoc {
98         return (byte)read();
99     }
100     
101     public int readUnsignedByte() throws IOException JavaDoc {
102         return read();
103     }
104
105     public char readChar() throws IOException JavaDoc {
106         return (char)readUnsignedShort();
107     }
108     
109     final long readLongByte() throws IOException JavaDoc {
110         return readUnsignedByte();
111     }
112     
113     //#ifdef SMALLDB
114
public long readLong() throws IOException JavaDoc {
115         long ret = 0;
116         long b;
117         int shift = 0;
118         do {
119             b = read();
120             ret |= ((b & 0x7f) << shift);
121             shift += 7;
122         } while ((b & 0x80) != 0);
123         return ret;
124     }
125
126     public int readInt() throws IOException JavaDoc {
127         int ret = 0;
128         int b;
129         int shift = 0;
130         do {
131             b = read();
132             ret |= ((b & 0x7f) << shift);
133             shift += 7;
134         } while ((b & 0x80) != 0);
135         return ret;
136     }
137
138     public short readShort() throws IOException JavaDoc {
139         return (short)readInt();
140     }
141
142     public int readUnsignedShort() throws IOException JavaDoc {
143         return readInt();
144     }
145     
146     //#else
147
//- public short readShort() throws IOException {
148
//- return (short)readUnsignedShort();
149
//- }
150
//-
151
//- public int readUnsignedShort() throws IOException {
152
//- int ret = readUnsignedByte() << 8;
153
//- ret += readUnsignedByte();
154
//- return ret;
155
//- }
156
//-
157
//- public int readInt() throws IOException {
158
//- int ret = readUnsignedByte() << 24;
159
//- ret |= (readUnsignedByte() << 16);
160
//- ret |= (readUnsignedByte() << 8);
161
//- ret |= readUnsignedByte();
162
//- return ret;
163
//- }
164
//-
165
//- public long readLong() throws IOException {
166
//- long ret = readLongByte() << 56;
167
//- ret |= (readLongByte() << 48);
168
//- ret |= (readLongByte() << 40);
169
//- ret |= (readLongByte() << 32);
170
//- ret |= (readLongByte() << 24);
171
//- ret |= (readLongByte() << 16);
172
//- ret |= (readLongByte() << 8);
173
//- ret |= readLongByte();
174
//- return ret;
175
//- }
176
//#endif
177

178     public float readFloat() throws IOException JavaDoc {
179         return Float.intBitsToFloat(readInt());
180     }
181     
182     public double readDouble() throws IOException JavaDoc {
183         return Double.longBitsToDouble(readLong());
184     }
185     
186     public String JavaDoc readLine() throws IOException JavaDoc {
187         InputStreamReader ns = new InputStreamReader(this);
188         BufferedReader JavaDoc r = new BufferedReader JavaDoc(ns);
189         return r.readLine();
190     }
191     
192     public String JavaDoc readUTF() throws IOException JavaDoc {
193         throw new IOException JavaDoc("readUTF not implemented");
194     }
195
196     final boolean fillBuffer() throws IOException JavaDoc {
197         boolean ret = pos >= len;
198         if (ret) {
199             len = in.read(buf, 0, buf.length);
200             pos = 0;
201             ret = pos >= len;
202         }
203         return ret;
204     }
205
206     public int read() throws IOException JavaDoc {
207         int ret = -1;
208         if (buffered) {
209             if (!fillBuffer()) {
210                 ret = buf[pos++] & 0xff;
211                 position++;
212             }
213         } else {
214             ret = in.read();
215             if (ret < 0) {
216                 throw new EOFException JavaDoc();
217             }
218             position++;
219         }
220         //#ifdef DEBUG
221
if (trace) Debug.println("DIS.read() = " + ret);
222         //#endif
223
return ret;
224     }
225
226     public int read(byte[] b, int off, int cnt) throws IOException JavaDoc {
227         int ret = 0;
228         //#ifdef DEBUG
229
int xoff = off;
230         int xcnt = cnt;
231         //#endif
232
if (buffered) {
233             while (cnt > 0 && !fillBuffer()) {
234                 int amt = len - pos;
235                 if (amt > cnt) amt = cnt;
236                 System.arraycopy(buf, pos, b, off, amt);
237                 cnt -= amt;
238                 off += amt;
239                 ret += amt;
240                 pos += amt;
241                 position += amt;
242             }
243         } else {
244             while (cnt > 0) {
245                 int amt = in.read(b, off, cnt);
246                 ret += amt;
247                 position += amt;
248                 off += amt;
249                 cnt -= amt;
250             }
251         }
252         //#ifdef DEBUG
253
if (trace) {
254             Debug.println("DIS.read() = " + ret + ": " +
255                           Util.hexBytes(b, xoff, ret));
256         }
257         //#endif
258
return ret;
259     }
260
261     //#ifdef DEBUG
262
static final boolean trace = false;
263     //#endif
264

265     public int read(byte[] buf) throws IOException JavaDoc {
266         return read(buf, 0, buf.length);
267     }
268
269     public long skip(long n) throws IOException JavaDoc {
270         long ret = 0;
271         if (buffered) {
272             while (n > 0 && !fillBuffer()) {
273                 long amt = len - pos;
274                 if (amt > n) amt = n;
275                 n -= amt;
276                 pos += amt;
277                 position += amt;
278             }
279         } else {
280             ret = in.skip(n);
281         }
282         return ret;
283     }
284 }
285
Popular Tags