1 46 50 package org.mr.core.util.byteable; 51 52 53 import java.io.EOFException ; 54 55 import java.io.IOException ; 56 57 import java.io.UTFDataFormatException ; 58 import java.io.UnsupportedEncodingException ; 59 import java.nio.ByteBuffer ; 60 61 70 public class ByteableInputStream { 71 72 private ByteBuffer buff = null; 73 74 private int mark_pos; 75 76 public ByteableInputStream(){ 77 78 } 79 80 81 88 public final int read(byte b[], int off, int len) { 89 int bytesToRead =len; 91 int bytesCanRead = buff.remaining(); 92 if(bytesToRead >bytesCanRead) 93 bytesToRead =bytesCanRead; 94 95 buff.get(b, off ,bytesToRead ); 96 return bytesToRead; 97 } 98 99 100 public final void mark(int pos){ 101 mark_pos = pos; 102 } 103 104 105 public final void reset() throws IOException { 106 buff.position(mark_pos); 107 } 108 109 110 147 public final int read(byte b[]) throws IOException { 148 return read(b, 0, b.length); 149 } 150 155 public final Byteable readByteable() throws IOException { 156 Byteable b = null; 157 158 String byteableName = readASCIIString(); 159 160 if(!byteableName.equals(ByteableRegistry.NULL_NAME)){ 161 Byteable factory = ByteableRegistry.getByteableFactory(byteableName); 162 if(factory != null){ 163 164 b = factory.createInstance(this); 165 }else{ 166 throw new IOException ("Can not read object -no byteable for byteable name="+byteableName+" byteableRegistry="+ByteableRegistry.desc()); 167 } 168 } 169 170 return b; 171 } 173 174 public final int available() throws IOException { 175 return buff.remaining(); } 177 178 192 public final int readUnsignedByte() throws IOException { 193 int ch = read(); 194 if (ch < 0) 195 throw new EOFException (); 196 return ch; 197 } 198 213 public final int readUnsignedShort() throws IOException { 214 int ch1 = read(); 215 int ch2 = read(); 216 if ((ch1 | ch2) < 0) 217 throw new EOFException (); 218 return (ch1 << 8) + (ch2 << 0); 219 220 } 221 222 223 224 225 239 public final void readFully(byte b[]) throws IOException { 240 readFully(b, 0, b.length); 241 } 242 243 259 public final void readFully(byte b[], int off, int len) throws IOException { 260 if (len < 0) 261 throw new IndexOutOfBoundsException (); 262 int n = 0; 263 while (n < len) { 264 int count = read(b, off + n, len - n); 265 if (count <= 0) 266 throw new EOFException (); 267 n += count; 268 } 269 } 270 271 272 byte[] asciiTemp = new byte[1000]; 273 char[] str = null; 274 280 public final String readASCIIString() throws IOException { 281 short length = readShort(); 282 283 if(length == ByteableOutputStream.NULL_PLACE_HOLDER){ 284 return null; 285 } 286 287 if (asciiTemp.length < length) { 288 asciiTemp = new byte[length]; 289 } 290 291 readFully(asciiTemp , 0 , length); 292 293 try { 294 return new String (asciiTemp, 0, length, "ascii"); 295 } catch (UnsupportedEncodingException e) { 296 } 298 299 if (str == null || str.length < asciiTemp.length) { 300 str = new char[asciiTemp.length]; 301 } 302 303 int ch1 ; 304 int asciiTempIndex = 0; 305 for (int i = 0; i < length; i++) { 306 ch1 = asciiTemp[asciiTempIndex++] & 0xff; 307 str[i] =(char)ch1; } 310 return new String (str , 0 ,length); 311 } 312 313 314 315 316 334 public final String readUTF() throws IOException { 335 int utflen = readInt(); 336 if(utflen ==ByteableOutputStream.NULL_PLACE_HOLDER){ 337 return null; 338 } 339 340 StringBuffer str = new StringBuffer (utflen); 341 byte bytearr [] = new byte[utflen]; 342 int c, char2, char3; 343 int count = 0; 344 345 readFully(bytearr, 0, utflen); 346 347 while (count < utflen) { 348 c = (int) bytearr[count] & 0xff; 349 switch (c >> 4) { 350 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 351 352 count++; 353 str.append((char)c); 354 break; 355 case 12: case 13: 356 357 count += 2; 358 if (count > utflen) 359 throw new UTFDataFormatException (); 360 char2 = (int) bytearr[count-1]; 361 if ((char2 & 0xC0) != 0x80) 362 throw new UTFDataFormatException (); 363 str.append((char)(((c & 0x1F) << 6) | (char2 & 0x3F))); 364 break; 365 case 14: 366 367 count += 3; 368 if (count > utflen) 369 throw new UTFDataFormatException (); 370 char2 = (int) bytearr[count-2]; 371 char3 = (int) bytearr[count-1]; 372 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) 373 throw new UTFDataFormatException (); 374 str.append((char)(((c & 0x0F) << 12) | 375 ((char2 & 0x3F) << 6) | 376 ((char3 & 0x3F) << 0))); 377 break; 378 default: 379 380 throw new UTFDataFormatException (); 381 } 382 } 383 String result = new String (str); 385 386 return result; 387 } 388 389 404 public final char readChar() throws IOException { 405 int ch1 = read(); 406 int ch2 = read(); 407 if ((ch1 | ch2) < 0) 408 throw new EOFException (); 409 return (char)((ch1 << 8) + (ch2 << 0)); 410 } 411 412 427 public final int readInt() throws IOException { 428 int ch1 = read(); 429 int ch2 = read(); 430 int ch3 = read(); 431 int ch4 = read(); 432 if ((ch1 | ch2 | ch3 | ch4) < 0) 433 throw new EOFException (); 434 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 435 } 436 437 451 public final byte readByte() throws IOException { 452 int ch = read(); 453 if (ch < 0) 454 throw new EOFException (); 455 return (byte)(ch); 456 } 457 458 471 public final boolean readBoolean() throws IOException { 472 int ch = read(); 473 if (ch < 0) 474 throw new EOFException (); 475 return (ch != 0); 476 } 477 478 private byte readBuffer[] = new byte[8]; 479 480 495 public final long readLong() throws IOException { 496 readFully(readBuffer, 0, 8); 497 return (((long)readBuffer[0] << 56) + 498 ((long)(readBuffer[1] & 255) << 48) + 499 ((long)(readBuffer[2] & 255) << 40) + 500 ((long)(readBuffer[3] & 255) << 32) + 501 ((long)(readBuffer[4] & 255) << 24) + 502 ((readBuffer[5] & 255) << 16) + 503 ((readBuffer[6] & 255) << 8) + 504 ((readBuffer[7] & 255) << 0)); 505 } 506 507 508 523 public final short readShort() throws IOException { 524 int ch1 = read(); 525 int ch2 = read(); 526 if ((ch1 | ch2) < 0) 527 throw new EOFException (); 528 return (short)((ch1 << 8) + (ch2 << 0)); 529 } 530 531 547 public final double readDouble() throws IOException { 548 return Double.longBitsToDouble(readLong()); 549 } 550 551 567 public final float readFloat() throws IOException { 568 return Float.intBitsToFloat(readInt()); 569 } 570 571 572 573 586 public final int read() throws IOException { 587 if(!buff.hasRemaining()) 588 return -1; 589 else 590 return buff.get() & 0xff; 591 592 } 593 594 595 596 public final void release() { 597 598 buff = null; 599 600 ByteableInputStreamPool.getInstance().releaseObject(this); 601 } 603 604 605 608 public final void setUnderLine(ByteBuffer buffer) throws IOException { 609 buff = buffer; 610 611 } 612 613 public final ByteBuffer getUnderlying() { 614 return this.buff; 615 } 616 } 617 | Popular Tags |