1 7 8 package java.io; 9 10 21 public 22 class DataInputStream extends FilterInputStream implements DataInput { 23 24 30 public DataInputStream(InputStream in) { 31 super(in); 32 } 33 34 37 private byte bytearr[] = new byte[80]; 38 private char chararr[] = new char[80]; 39 40 79 public final int read(byte b[]) throws IOException { 80 return in.read(b, 0, b.length); 81 } 82 83 133 public final int read(byte b[], int off, int len) throws IOException { 134 return in.read(b, off, len); 135 } 136 137 151 public final void readFully(byte b[]) throws IOException { 152 readFully(b, 0, b.length); 153 } 154 155 171 public final void readFully(byte b[], int off, int len) throws IOException { 172 if (len < 0) 173 throw new IndexOutOfBoundsException (); 174 int n = 0; 175 while (n < len) { 176 int count = in.read(b, off + n, len - n); 177 if (count < 0) 178 throw new EOFException (); 179 n += count; 180 } 181 } 182 183 195 public final int skipBytes(int n) throws IOException { 196 int total = 0; 197 int cur = 0; 198 199 while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) { 200 total += cur; 201 } 202 203 return total; 204 } 205 206 219 public final boolean readBoolean() throws IOException { 220 int ch = in.read(); 221 if (ch < 0) 222 throw new EOFException (); 223 return (ch != 0); 224 } 225 226 240 public final byte readByte() throws IOException { 241 int ch = in.read(); 242 if (ch < 0) 243 throw new EOFException (); 244 return (byte)(ch); 245 } 246 247 261 public final int readUnsignedByte() throws IOException { 262 int ch = in.read(); 263 if (ch < 0) 264 throw new EOFException (); 265 return ch; 266 } 267 268 283 public final short readShort() throws IOException { 284 int ch1 = in.read(); 285 int ch2 = in.read(); 286 if ((ch1 | ch2) < 0) 287 throw new EOFException (); 288 return (short)((ch1 << 8) + (ch2 << 0)); 289 } 290 291 306 public final int readUnsignedShort() throws IOException { 307 int ch1 = in.read(); 308 int ch2 = in.read(); 309 if ((ch1 | ch2) < 0) 310 throw new EOFException (); 311 return (ch1 << 8) + (ch2 << 0); 312 } 313 314 329 public final char readChar() throws IOException { 330 int ch1 = in.read(); 331 int ch2 = in.read(); 332 if ((ch1 | ch2) < 0) 333 throw new EOFException (); 334 return (char)((ch1 << 8) + (ch2 << 0)); 335 } 336 337 352 public final int readInt() throws IOException { 353 int ch1 = in.read(); 354 int ch2 = in.read(); 355 int ch3 = in.read(); 356 int ch4 = in.read(); 357 if ((ch1 | ch2 | ch3 | ch4) < 0) 358 throw new EOFException (); 359 return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 360 } 361 362 private byte readBuffer[] = new byte[8]; 363 364 379 public final long readLong() throws IOException { 380 readFully(readBuffer, 0, 8); 381 return (((long)readBuffer[0] << 56) + 382 ((long)(readBuffer[1] & 255) << 48) + 383 ((long)(readBuffer[2] & 255) << 40) + 384 ((long)(readBuffer[3] & 255) << 32) + 385 ((long)(readBuffer[4] & 255) << 24) + 386 ((readBuffer[5] & 255) << 16) + 387 ((readBuffer[6] & 255) << 8) + 388 ((readBuffer[7] & 255) << 0)); 389 } 390 391 407 public final float readFloat() throws IOException { 408 return Float.intBitsToFloat(readInt()); 409 } 410 411 427 public final double readDouble() throws IOException { 428 return Double.longBitsToDouble(readLong()); 429 } 430 431 private char lineBuffer[]; 432 433 460 @Deprecated 461 public final String readLine() throws IOException { 462 char buf[] = lineBuffer; 463 464 if (buf == null) { 465 buf = lineBuffer = new char[128]; 466 } 467 468 int room = buf.length; 469 int offset = 0; 470 int c; 471 472 loop: while (true) { 473 switch (c = in.read()) { 474 case -1: 475 case '\n': 476 break loop; 477 478 case '\r': 479 int c2 = in.read(); 480 if ((c2 != '\n') && (c2 != -1)) { 481 if (!(in instanceof PushbackInputStream )) { 482 this.in = new PushbackInputStream (in); 483 } 484 ((PushbackInputStream )in).unread(c2); 485 } 486 break loop; 487 488 default: 489 if (--room < 0) { 490 buf = new char[offset + 128]; 491 room = buf.length - offset - 1; 492 System.arraycopy(lineBuffer, 0, buf, 0, offset); 493 lineBuffer = buf; 494 } 495 buf[offset++] = (char) c; 496 break; 497 } 498 } 499 if ((c == -1) && (offset == 0)) { 500 return null; 501 } 502 return String.copyValueOf(buf, 0, offset); 503 } 504 505 521 public final String readUTF() throws IOException { 522 return readUTF(this); 523 } 524 525 544 public final static String readUTF(DataInput in) throws IOException { 545 int utflen = in.readUnsignedShort(); 546 byte[] bytearr = null; 547 char[] chararr = null; 548 if (in instanceof DataInputStream ) { 549 DataInputStream dis = (DataInputStream )in; 550 if (dis.bytearr.length < utflen){ 551 dis.bytearr = new byte[utflen*2]; 552 dis.chararr = new char[utflen*2]; 553 } 554 chararr = dis.chararr; 555 bytearr = dis.bytearr; 556 } else { 557 bytearr = new byte[utflen]; 558 chararr = new char[utflen]; 559 } 560 561 int c, char2, char3; 562 int count = 0; 563 int chararr_count=0; 564 565 in.readFully(bytearr, 0, utflen); 566 567 while (count < utflen) { 568 c = (int) bytearr[count] & 0xff; 569 if (c > 127) break; 570 count++; 571 chararr[chararr_count++]=(char)c; 572 } 573 574 while (count < utflen) { 575 c = (int) bytearr[count] & 0xff; 576 switch (c >> 4) { 577 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 578 579 count++; 580 chararr[chararr_count++]=(char)c; 581 break; 582 case 12: case 13: 583 584 count += 2; 585 if (count > utflen) 586 throw new UTFDataFormatException ( 587 "malformed input: partial character at end"); 588 char2 = (int) bytearr[count-1]; 589 if ((char2 & 0xC0) != 0x80) 590 throw new UTFDataFormatException ( 591 "malformed input around byte " + count); 592 chararr[chararr_count++]=(char)(((c & 0x1F) << 6) | 593 (char2 & 0x3F)); 594 break; 595 case 14: 596 597 count += 3; 598 if (count > utflen) 599 throw new UTFDataFormatException ( 600 "malformed input: partial character at end"); 601 char2 = (int) bytearr[count-2]; 602 char3 = (int) bytearr[count-1]; 603 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) 604 throw new UTFDataFormatException ( 605 "malformed input around byte " + (count-1)); 606 chararr[chararr_count++]=(char)(((c & 0x0F) << 12) | 607 ((char2 & 0x3F) << 6) | 608 ((char3 & 0x3F) << 0)); 609 break; 610 default: 611 612 throw new UTFDataFormatException ( 613 "malformed input around byte " + count); 614 } 615 } 616 return new String (chararr, 0, chararr_count); 618 } 619 } 620 | Popular Tags |