Quadcap Embeddable Database

com/quadcap/sql/io/DataInputStream.java

Go to the documentation of this file.
00001 package com.quadcap.sql.io; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * Implement low level serialization 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 //#ifdef SMALLDB 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 //#else 00147 //- public short readShort() throws IOException { 00148 //- return (short)readUnsignedShort(); 00149 //- } 00150 //- 00151 //- public int readUnsignedShort() throws IOException { 00152 //- int ret = readUnsignedByte() << 8; 00153 //- ret += readUnsignedByte(); 00154 //- return ret; 00155 //- } 00156 //- 00157 //- public int readInt() throws IOException { 00158 //- int ret = readUnsignedByte() << 24; 00159 //- ret |= (readUnsignedByte() << 16); 00160 //- ret |= (readUnsignedByte() << 8); 00161 //- ret |= readUnsignedByte(); 00162 //- return ret; 00163 //- } 00164 //- 00165 //- public long readLong() throws IOException { 00166 //- long ret = readLongByte() << 56; 00167 //- ret |= (readLongByte() << 48); 00168 //- ret |= (readLongByte() << 40); 00169 //- ret |= (readLongByte() << 32); 00170 //- ret |= (readLongByte() << 24); 00171 //- ret |= (readLongByte() << 16); 00172 //- ret |= (readLongByte() << 8); 00173 //- ret |= readLongByte(); 00174 //- return ret; 00175 //- } 00176 //#endif 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 //#ifdef DEBUG 00221 if (trace) Debug.println("DIS.read() = " + ret); 00222 //#endif 00223 return ret; 00224 } 00225 00226 public int read(byte[] b, int off, int cnt) throws IOException { 00227 int ret = 0; 00228 //#ifdef DEBUG 00229 int xoff = off; 00230 int xcnt = cnt; 00231 //#endif 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 //#ifdef DEBUG 00253 if (trace) { 00254 Debug.println("DIS.read() = " + ret + ": " + 00255 Util.hexBytes(b, xoff, ret)); 00256 } 00257 //#endif 00258 return ret; 00259 } 00260 00261 //#ifdef DEBUG 00262 static final boolean trace = false; 00263 //#endif 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 }