1 30 31 32 package org.hsqldb.lib; 33 34 import java.io.DataInput ; 35 import java.io.EOFException ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 39 47 public class HsqlByteArrayInputStream extends InputStream 48 implements DataInput { 49 50 protected byte[] buf; 51 protected int pos; 52 protected int mark = 0; 53 protected int count; 54 55 public HsqlByteArrayInputStream(byte[] buf) { 56 57 this.buf = buf; 58 this.pos = 0; 59 this.count = buf.length; 60 } 61 62 public HsqlByteArrayInputStream(byte[] buf, int offset, int length) { 63 64 this.buf = buf; 65 this.pos = offset; 66 this.count = Math.min(offset + length, buf.length); 67 this.mark = offset; 68 } 69 70 public final void readFully(byte[] b) throws IOException { 72 readFully(b, 0, b.length); 73 } 74 75 public final void readFully(byte[] b, int off, 76 int len) throws IOException { 77 78 if (len < 0) { 79 throw new IndexOutOfBoundsException (); 80 } 81 82 int n = 0; 83 84 while (n < len) { 85 int count = read(b, off + n, len - n); 86 87 if (count < 0) { 88 throw new EOFException (); 89 } 90 91 n += count; 92 } 93 } 94 95 public final boolean readBoolean() throws IOException { 96 97 int ch = read(); 98 99 if (ch < 0) { 100 throw new EOFException (); 101 } 102 103 return (ch != 0); 104 } 105 106 public final byte readByte() throws IOException { 107 108 int ch = read(); 109 110 if (ch < 0) { 111 throw new EOFException (); 112 } 113 114 return (byte) ch; 115 } 116 117 public final int readUnsignedByte() throws IOException { 118 119 int ch = read(); 120 121 if (ch < 0) { 122 throw new EOFException (); 123 } 124 125 return ch; 126 } 127 128 public short readShort() throws IOException { 129 130 if (count - pos < 2) { 131 pos = count; 132 133 throw new EOFException (); 134 } 135 136 int ch1 = buf[pos++] & 0xff; 137 int ch2 = buf[pos++] & 0xff; 138 139 return (short) ((ch1 << 8) + (ch2)); 140 } 141 142 public final int readUnsignedShort() throws IOException { 143 144 int ch1 = read(); 145 int ch2 = read(); 146 147 if ((ch1 | ch2) < 0) { 148 throw new EOFException (); 149 } 150 151 return (ch1 << 8) + (ch2); 152 } 153 154 public final char readChar() throws IOException { 155 156 int ch1 = read(); 157 int ch2 = read(); 158 159 if ((ch1 | ch2) < 0) { 160 throw new EOFException (); 161 } 162 163 return (char) ((ch1 << 8) + (ch2)); 164 } 165 166 public int readInt() throws IOException { 167 168 if (count - pos < 4) { 169 pos = count; 170 171 throw new EOFException (); 172 } 173 174 int ch1 = buf[pos++] & 0xff; 175 int ch2 = buf[pos++] & 0xff; 176 int ch3 = buf[pos++] & 0xff; 177 int ch4 = buf[pos++] & 0xff; 178 179 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4)); 180 } 181 182 public final long readLong() throws IOException { 183 return (((long) readInt()) << 32) 184 + (((long) readInt()) & 0xffffffffL); 185 } 186 187 public final float readFloat() throws IOException { 188 return Float.intBitsToFloat(readInt()); 189 } 190 191 public final double readDouble() throws IOException { 192 return Double.longBitsToDouble(readLong()); 193 } 194 195 public int skipBytes(int n) throws IOException { 196 return (int) skip(n); 197 } 198 199 public String readLine() throws IOException { 200 201 202 throw new java.lang.RuntimeException ("not implemented."); 203 } 204 205 public String readUTF() throws IOException { 206 207 int bytecount = readUnsignedShort(); 208 209 if (pos + bytecount >= count) { 210 throw new EOFException (); 211 } 212 213 String result = StringConverter.readUTF(buf, pos, bytecount); 214 215 pos += bytecount; 216 217 return result; 218 } 219 220 public int read() { 222 return (pos < count) ? (buf[pos++] & 0xff) 223 : -1; 224 } 225 226 public int read(byte[] b, int off, int len) { 227 228 if (pos >= count) { 229 return -1; 230 } 231 232 if (pos + len > count) { 233 len = count - pos; 234 } 235 236 if (len <= 0) { 237 return 0; 238 } 239 240 System.arraycopy(buf, pos, b, off, len); 241 242 pos += len; 243 244 return len; 245 } 246 247 public long skip(long n) { 248 249 if (pos + n > count) { 250 n = count - pos; 251 } 252 253 if (n < 0) { 254 return 0; 255 } 256 257 pos += n; 258 259 return n; 260 } 261 262 public int available() { 263 return count - pos; 264 } 265 266 public boolean markSupported() { 267 return true; 268 } 269 270 public void mark(int readAheadLimit) { 271 mark = pos; 272 } 273 274 public void reset() { 275 pos = mark; 276 } 277 278 public void close() throws IOException {} 279 } 280 | Popular Tags |