1 32 package com.imagero.uio; 33 34 import com.imagero.uio.buffer.arrays.AbstractArrayBufferManager; 35 import com.imagero.uio.buffer.arrays.CharArrayBufferManager; 36 import com.imagero.uio.buffer.arrays.DoubleArrayBufferManager; 37 import com.imagero.uio.buffer.arrays.FloatArrayBufferManager; 38 import com.imagero.uio.buffer.arrays.IntArrayBufferManager; 39 import com.imagero.uio.buffer.arrays.LongArrayBufferManager; 40 import com.imagero.uio.buffer.arrays.ShortArrayBufferManager; 41 42 import java.io.DataInputStream ; 43 import java.io.EOFException ; 44 import java.io.IOException ; 45 46 51 public abstract class AbstractRandomAccessRO implements RandomAccessRO { 52 53 protected abstract int _read() throws IOException ; 54 55 private DataInputBE dibe = new DataInputBE(); 56 private DataInputLE dile = new DataInputLE(); 57 58 EDataInput dataInput; 59 60 int byteOrder; 61 62 public final byte readByte() throws IOException { 63 return (byte) (_read()); 64 } 65 66 public final int readUnsignedByte() throws IOException { 67 return _read(); 68 } 69 70 public boolean readBoolean() throws IOException { 71 return (_read() != 0); 72 } 73 74 public float readFloat() throws IOException { 75 return Float.intBitsToFloat(readInt()); 76 } 77 78 public double readDouble() throws IOException { 79 return Double.longBitsToDouble(readLong()); 80 } 81 82 public int skipBytes(int n) throws IOException { 83 if (n <= 0) { 84 return 0; 85 } 86 long len = length(); 87 long pos = getFilePointer(); 88 long newpos = pos + n; 89 if (newpos > len) { 90 newpos = len; 91 } 92 seek(newpos); 93 return (int) (newpos - pos); 94 } 95 96 public void readFully(byte[] b) throws IOException { 97 readFully(b, 0, b.length); 98 } 99 100 public void readFully(byte[] b, int off, int len) throws IOException { 101 int n = 0; 102 do { 103 int count = read(b, off + n, len - n); 104 if (count < 0) { 105 throw new EOFException (); 106 } 107 n += count; 108 } 109 while (n < len); 110 } 111 112 public void readFully(short[] dest) throws IOException { 113 readFully(dest, getByteOrder()); 114 } 115 116 public void readFully(short[] dest, int byteOrder) throws IOException { 117 readFully(dest, 0, dest.length, byteOrder); 118 } 119 120 public void readFully(short[] dest, int destOffset, int len) throws IOException { 121 readFully(dest, destOffset, len, getByteOrder()); 122 } 123 124 public void readFully(short[] dest, int destOffset, int len, int byteOrder) throws IOException { 125 AbstractArrayBufferManager sm = new ShortArrayBufferManager(dest); 126 writeTo(sm, destOffset, len, byteOrder); 127 } 128 129 public void readFully(char[] dest) throws IOException { 130 readFully(dest, getByteOrder()); 131 } 132 133 public void readFully(char[] dest, int byteOrder) throws IOException { 134 readFully(dest, 0, dest.length, byteOrder); 135 } 136 137 public void readFully(char[] dest, int destOffset, int len) throws IOException { 138 readFully(dest, destOffset, len, getByteOrder()); 139 } 140 141 public void readFully(char[] dest, int destOffset, int len, int byteOrder) throws IOException { 142 AbstractArrayBufferManager sm = new CharArrayBufferManager(dest, 0, len); 143 writeTo(sm, destOffset, len, byteOrder); 144 } 145 146 147 public void readFully(int[] dest) throws IOException { 148 readFully(dest, getByteOrder()); 149 } 150 151 public void readFully(int[] dest, int byteOrder) throws IOException { 152 readFully(dest, 0, dest.length, byteOrder); 153 } 154 155 public void readFully(int[] dest, int destOffset, int len) throws IOException { 156 readFully(dest, destOffset, len, getByteOrder()); 157 } 158 159 public void readFully(int[] dest, int destOffset, int len, int byteOrder) throws IOException { 160 AbstractArrayBufferManager sm = new IntArrayBufferManager(dest, 0, len); 161 writeTo(sm, destOffset, len, byteOrder); 162 } 163 164 public void readFully(float[] dest) throws IOException { 165 readFully(dest, getByteOrder()); 166 } 167 168 public void readFully(float[] dest, int byteOrder) throws IOException { 169 readFully(dest, 0, dest.length, byteOrder); 170 } 171 172 public void readFully(float[] dest, int destOffset, int len) throws IOException { 173 readFully(dest, destOffset, len, getByteOrder()); 174 } 175 176 public void readFully(float[] dest, int destOffset, int len, int byteOrder) throws IOException { 177 AbstractArrayBufferManager sm = new FloatArrayBufferManager(dest); 178 writeTo(sm, destOffset, len, byteOrder); 179 } 180 181 public void readFully(long[] dest) throws IOException { 182 readFully(dest, getByteOrder()); 183 } 184 185 public void readFully(long[] dest, int byteOrder) throws IOException { 186 readFully(dest, 0, dest.length, byteOrder); 187 } 188 189 public void readFully(long[] dest, int destOffset, int len) throws IOException { 190 readFully(dest, destOffset, len, getByteOrder()); 191 } 192 193 public void readFully(long[] dest, int destOffset, int len, int byteOrder) throws IOException { 194 AbstractArrayBufferManager sm = new LongArrayBufferManager(dest); 195 writeTo(sm, destOffset, len, byteOrder); 196 } 197 198 public void readFully(double[] dest) throws IOException { 199 readFully(dest, getByteOrder()); 200 } 201 202 public void readFully(double[] dest, int byteOrder) throws IOException { 203 readFully(dest, 0, dest.length, byteOrder); 204 } 205 206 public void readFully(double[] dest, int destOffset, int len) throws IOException { 207 readFully(dest, destOffset, len, getByteOrder()); 208 } 209 210 public void readFully(double[] dest, int destOffset, int len, int byteOrder) throws IOException { 211 AbstractArrayBufferManager sm = new DoubleArrayBufferManager(dest); 212 writeTo(sm, destOffset, len, byteOrder); 213 } 214 215 private void writeTo(AbstractArrayBufferManager sm, int destOffset, int len, int byteOrder) throws IOException { 216 int length = len * sm.getUnitSize(); 217 int offset = destOffset * sm.getUnitSize(); 218 byte[] b = new byte[length]; 219 readFully(b); 220 RandomAccess ra = new RandomAccessBuffer(sm, byteOrder); 221 ra.write(b, offset, length); 222 ra.close(); 223 } 224 225 public String readLine() throws IOException { 226 return new String (readByteLine()); 227 } 228 229 public byte[] readByteLine() throws IOException { 230 long start = getFilePointer(); 231 long end = start; 232 boolean finished = false; 233 int length = 0; 234 while (!finished) { 235 switch (read()) { 236 case -1: 237 case '\n': 238 finished = true; 239 end = getFilePointer(); 240 length = (int) (end - start); 241 break; 242 case '\r': 243 finished = true; 244 end = getFilePointer(); 245 length = (int) (end - start); 246 if ((read()) == '\n') { 247 end = getFilePointer(); 248 } 249 break; 250 } 251 } 252 byte[] b = new byte[length]; 253 seek(start); 254 readFully(b); 255 seek(end); 256 return b; 257 } 258 259 public String readUTF() throws IOException { 260 return DataInputStream.readUTF(this); 261 } 262 263 public short readShort() throws IOException { 264 return dataInput.readShort(); 265 } 266 267 public int readUnsignedShort() throws IOException { 268 return dataInput.readUnsignedShort(); 269 } 270 271 public char readChar() throws IOException { 272 return dataInput.readChar(); 273 } 274 275 public int readInt() throws IOException { 276 return dataInput.readInt(); 277 } 278 279 public long readLong() throws IOException { 280 return dataInput.readLong(); 281 } 282 283 288 protected void _setByteOrder(int byteOrder) throws IOException { 289 if (byteOrder == RandomAccessFactory.AUTO_ENDIAN) { 290 byteOrder = readByteOrder(); 291 } 292 if (this.byteOrder == byteOrder) { 293 if (this.dataInput != null) { 294 return; 295 } 296 } 297 switch (byteOrder) { 298 case BIG_ENDIAN: 299 this.byteOrder = byteOrder; 300 dataInput = dibe; 301 break; 302 case LITTLE_ENDIAN: 303 this.byteOrder = byteOrder; 304 dataInput = dile; 305 break; 306 default: 307 this.byteOrder = BIG_ENDIAN; 308 dataInput = dibe; 309 } 310 } 311 312 public void setByteOrder(int byteOrder) throws IOException { 313 if (this.byteOrder == RandomAccessFactory.AUTO_ENDIAN) { 314 this.byteOrder = readByteOrder(); 315 } 316 if (this.byteOrder == byteOrder) { 317 if (this.dataInput != null) { 318 return; 319 } 320 } 321 switch (byteOrder) { 322 case BIG_ENDIAN: 323 this.byteOrder = byteOrder; 324 dataInput = dibe; 325 break; 326 case LITTLE_ENDIAN: 327 this.byteOrder = byteOrder; 328 dataInput = dile; 329 break; 330 default: 331 throw new RuntimeException ("Wrong byteOrder:" + Integer.toHexString(byteOrder)); 332 } 333 } 334 335 340 protected int readByteOrder() throws IOException { 341 long pos = getFilePointer(); 342 seek(0); 343 int byteOrder = (_read() << 8) | _read(); 344 seek(pos); 345 return byteOrder == LITTLE_ENDIAN ? byteOrder : BIG_ENDIAN; 346 } 347 348 public int getByteOrder() { 349 return byteOrder; 350 } 351 352 class DataInputLE implements EDataInput { 353 public short readShort() throws IOException { 354 int ch = _read() | (_read() << 8); 355 return (short) ch; 356 } 357 358 public int readUnsignedShort() throws IOException { 359 int ch = _read() | (_read() << 8); 360 return ch; 361 } 362 363 public char readChar() throws IOException { 364 int ch = _read() | (_read() << 8); 365 return (char) ch; 366 } 367 368 public int readInt() throws IOException { 369 return _read() | (_read() << 8) | (_read() << 16) | (_read() << 24); 370 } 371 372 public long readLong() throws IOException { 373 return (readInt() & 0xFFFFFFFFL) | ((long) readInt() << 32); 374 } 375 } 376 377 class DataInputBE implements EDataInput { 378 public short readShort() throws IOException { 379 return (short) ((_read() << 8) | _read()); 380 } 381 382 public int readUnsignedShort() throws IOException { 383 return (_read() << 8) | _read(); 384 } 385 386 public char readChar() throws IOException { 387 return (char) ((_read() << 8) | _read()); 388 } 389 390 public int readInt() throws IOException { 391 return (_read() << 24) | (_read() << 16) | (_read() << 8) | _read(); 392 } 393 394 public long readLong() throws IOException { 395 return (((long) readInt()) << 32) + (readInt() & 0xFFFFFFFFL); 396 } 397 } 398 } 399 | Popular Tags |