00001
package com.quadcap.sql.io;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.BufferedReader;
00042
import java.io.DataInput;
00043
import java.io.EOFException;
00044
import java.io.InputStream;
00045
import java.io.IOException;
00046
00047
import com.quadcap.io.InputStreamReader;
00048
00049
import com.quadcap.util.Debug;
00050
import com.quadcap.util.Util;
00051
00052
00053
00054
00055 public class DataInputStream extends InputStream implements DataInput {
00056 InputStream
in;
00057 static final boolean buffered =
false;
00058
00059 static final int MAX = 4096;
00060 byte[]
buf =
new byte[
MAX];
00061 int pos;
00062 int len;
00063
00064 public DataInputStream(InputStream in) {
00065
this.in = in;
00066
this.pos = 0;
00067
this.len = 0;
00068 }
00069
00070 long position;
00071 public void setPosition(
long p) {
this.position = p; }
00072 public long getPosition() {
return position; }
00073
00074 public void setInputStream(InputStream in) {
00075
this.in = in;
00076 }
00077 public InputStream
getInputStream() {
00078
return in;
00079 }
00080
00081 public void readFully(byte[] b)
throws IOException {
00082
read(b, 0, b.length);
00083 }
00084
00085 public void readFully(byte[] b,
int off,
int len)
throws IOException {
00086
read(b, off,
len);
00087 }
00088
00089 public int skipBytes(
int n)
throws IOException {
00090
return (
int)
skip(n);
00091 }
00092
00093 public boolean readBoolean() throws IOException {
00094
return read() == 1;
00095 }
00096
00097 public byte
readByte() throws IOException {
00098
return (byte)
read();
00099 }
00100
00101 public int readUnsignedByte() throws IOException {
00102
return read();
00103 }
00104
00105 public char readChar() throws IOException {
00106
return (
char)
readUnsignedShort();
00107 }
00108
00109 final long readLongByte() throws IOException {
00110
return readUnsignedByte();
00111 }
00112
00113
00114 public long readLong() throws IOException {
00115
long ret = 0;
00116
long b;
00117
int shift = 0;
00118
do {
00119 b =
read();
00120 ret |= ((b & 0x7f) << shift);
00121 shift += 7;
00122 }
while ((b & 0x80) != 0);
00123
return ret;
00124 }
00125
00126 public int readInt() throws IOException {
00127
int ret = 0;
00128
int b;
00129
int shift = 0;
00130
do {
00131 b =
read();
00132 ret |= ((b & 0x7f) << shift);
00133 shift += 7;
00134 }
while ((b & 0x80) != 0);
00135
return ret;
00136 }
00137
00138 public short readShort() throws IOException {
00139
return (
short)
readInt();
00140 }
00141
00142 public int readUnsignedShort() throws IOException {
00143
return readInt();
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 public float readFloat() throws IOException {
00179
return Float.intBitsToFloat(
readInt());
00180 }
00181
00182 public double readDouble() throws IOException {
00183
return Double.longBitsToDouble(
readLong());
00184 }
00185
00186 public String
readLine() throws IOException {
00187
InputStreamReader ns =
new InputStreamReader(
this);
00188 BufferedReader r =
new BufferedReader(ns);
00189
return r.readLine();
00190 }
00191
00192 public String
readUTF() throws IOException {
00193
throw new IOException(
"readUTF not implemented");
00194 }
00195
00196 final boolean fillBuffer() throws IOException {
00197
boolean ret =
pos >=
len;
00198
if (ret) {
00199 len =
in.read(
buf, 0,
buf.length);
00200 pos = 0;
00201 ret = pos >= len;
00202 }
00203
return ret;
00204 }
00205
00206 public int read() throws IOException {
00207
int ret = -1;
00208
if (
buffered) {
00209
if (!
fillBuffer()) {
00210 ret =
buf[
pos++] & 0xff;
00211
position++;
00212 }
00213 }
else {
00214 ret =
in.read();
00215
if (ret < 0) {
00216
throw new EOFException();
00217 }
00218
position++;
00219 }
00220
00221
if (
trace)
Debug.println(
"DIS.read() = " + ret);
00222
00223
return ret;
00224 }
00225
00226 public int read(byte[] b,
int off,
int cnt)
throws IOException {
00227
int ret = 0;
00228
00229
int xoff = off;
00230
int xcnt = cnt;
00231
00232
if (
buffered) {
00233
while (cnt > 0 && !
fillBuffer()) {
00234
int amt =
len -
pos;
00235
if (amt > cnt) amt = cnt;
00236 System.arraycopy(
buf, pos, b, off, amt);
00237 cnt -= amt;
00238 off += amt;
00239 ret += amt;
00240 pos += amt;
00241
position += amt;
00242 }
00243 }
else {
00244
while (cnt > 0) {
00245
int amt =
in.read(b, off, cnt);
00246 ret += amt;
00247
position += amt;
00248 off += amt;
00249 cnt -= amt;
00250 }
00251 }
00252
00253
if (
trace) {
00254
Debug.println(
"DIS.read() = " + ret +
": " +
00255
Util.hexBytes(b, xoff, ret));
00256 }
00257
00258
return ret;
00259 }
00260
00261
00262 static final boolean trace =
false;
00263
00264
00265 public int read(byte[] buf)
throws IOException {
00266
return read(
buf, 0,
buf.length);
00267 }
00268
00269 public long skip(
long n)
throws IOException {
00270
long ret = 0;
00271
if (
buffered) {
00272
while (n > 0 && !
fillBuffer()) {
00273
long amt =
len -
pos;
00274
if (amt > n) amt = n;
00275 n -= amt;
00276 pos += amt;
00277
position += amt;
00278 }
00279 }
else {
00280 ret =
in.skip(n);
00281 }
00282
return ret;
00283 }
00284 }