1 package com.quadcap.sql.io; 2 3 40 41 import java.io.BufferedReader ; 42 import java.io.DataInput ; 43 import java.io.EOFException ; 44 import java.io.InputStream ; 45 import java.io.IOException ; 46 47 import com.quadcap.io.InputStreamReader; 48 49 import com.quadcap.util.Debug; 50 import com.quadcap.util.Util; 51 52 55 public class DataInputStream extends InputStream implements DataInput { 56 InputStream 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 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 in) { 75 this.in = in; 76 } 77 public InputStream getInputStream() { 78 return in; 79 } 80 81 public void readFully(byte[] b) throws IOException { 82 read(b, 0, b.length); 83 } 84 85 public void readFully(byte[] b, int off, int len) throws IOException { 86 read(b, off, len); 87 } 88 89 public int skipBytes(int n) throws IOException { 90 return (int)skip(n); 91 } 92 93 public boolean readBoolean() throws IOException { 94 return read() == 1; 95 } 96 97 public byte readByte() throws IOException { 98 return (byte)read(); 99 } 100 101 public int readUnsignedByte() throws IOException { 102 return read(); 103 } 104 105 public char readChar() throws IOException { 106 return (char)readUnsignedShort(); 107 } 108 109 final long readLongByte() throws IOException { 110 return readUnsignedByte(); 111 } 112 113 public long readLong() throws IOException { 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 { 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 { 139 return (short)readInt(); 140 } 141 142 public int readUnsignedShort() throws IOException { 143 return readInt(); 144 } 145 146 178 public float readFloat() throws IOException { 179 return Float.intBitsToFloat(readInt()); 180 } 181 182 public double readDouble() throws IOException { 183 return Double.longBitsToDouble(readLong()); 184 } 185 186 public String readLine() throws IOException { 187 InputStreamReader ns = new InputStreamReader(this); 188 BufferedReader r = new BufferedReader (ns); 189 return r.readLine(); 190 } 191 192 public String readUTF() throws IOException { 193 throw new IOException ("readUTF not implemented"); 194 } 195 196 final boolean fillBuffer() throws IOException { 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 { 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 (); 217 } 218 position++; 219 } 220 if (trace) Debug.println("DIS.read() = " + ret); 222 return ret; 224 } 225 226 public int read(byte[] b, int off, int cnt) throws IOException { 227 int ret = 0; 228 int xoff = off; 230 int xcnt = cnt; 231 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 if (trace) { 254 Debug.println("DIS.read() = " + ret + ": " + 255 Util.hexBytes(b, xoff, ret)); 256 } 257 return ret; 259 } 260 261 static final boolean trace = false; 263 265 public int read(byte[] buf) throws IOException { 266 return read(buf, 0, buf.length); 267 } 268 269 public long skip(long n) throws IOException { 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 |