1 8 9 package com.sleepycat.bind.tuple; 10 11 import java.math.BigInteger ; 12 13 import com.sleepycat.util.FastInputStream; 14 import com.sleepycat.util.PackedInteger; 15 import com.sleepycat.util.UtfOps; 16 17 76 public class TupleInput extends FastInputStream { 77 78 87 public TupleInput(byte[] buffer) { 88 89 super(buffer); 90 } 91 92 105 public TupleInput(byte[] buffer, int offset, int length) { 106 107 super(buffer, offset, length); 108 } 109 110 118 public TupleInput(TupleOutput output) { 119 120 super(output.getBufferBytes(), output.getBufferOffset(), 121 output.getBufferLength()); 122 } 123 124 126 139 public final String readString() 140 throws IndexOutOfBoundsException , IllegalArgumentException { 141 142 byte[] myBuf = buf; 143 int myOff = off; 144 if (available() >= 2 && 145 myBuf[myOff] == TupleOutput.NULL_STRING_UTF_VALUE && 146 myBuf[myOff + 1] == 0) { 147 skip(2); 148 return null; 149 } else { 150 int byteLen = UtfOps.getZeroTerminatedByteLength(myBuf, myOff); 151 skip(byteLen + 1); 152 return UtfOps.bytesToString(myBuf, myOff, byteLen); 153 } 154 } 155 156 165 public final char readChar() 166 throws IndexOutOfBoundsException { 167 168 return (char) readUnsignedShort(); 169 } 170 171 181 public final boolean readBoolean() 182 throws IndexOutOfBoundsException { 183 184 int c = readFast(); 185 if (c < 0) { 186 throw new IndexOutOfBoundsException (); 187 } 188 return (c != 0); 189 } 190 191 200 public final byte readByte() 201 throws IndexOutOfBoundsException { 202 203 return (byte) (readUnsignedByte() ^ 0x80); 204 } 205 206 215 public final short readShort() 216 throws IndexOutOfBoundsException { 217 218 return (short) (readUnsignedShort() ^ 0x8000); 219 } 220 221 230 public final int readInt() 231 throws IndexOutOfBoundsException { 232 233 return (int) (readUnsignedInt() ^ 0x80000000); 234 } 235 236 245 public final long readLong() 246 throws IndexOutOfBoundsException { 247 248 return readUnsignedLong() ^ 0x8000000000000000L; 249 } 250 251 268 public final float readFloat() 269 throws IndexOutOfBoundsException { 270 271 return Float.intBitsToFloat((int) readUnsignedInt()); 272 } 273 274 291 public final double readDouble() 292 throws IndexOutOfBoundsException { 293 294 return Double.longBitsToDouble(readUnsignedLong()); 295 } 296 297 317 public final float readSortedFloat() 318 throws IndexOutOfBoundsException { 319 320 int val = (int) readUnsignedInt(); 321 val ^= (val < 0) ? 0x80000000 : 0xffffffff; 322 return Float.intBitsToFloat(val); 323 } 324 325 345 public final double readSortedDouble() 346 throws IndexOutOfBoundsException { 347 348 long val = readUnsignedLong(); 349 val ^= (val < 0) ? 0x8000000000000000L : 0xffffffffffffffffL; 350 return Double.longBitsToDouble(val); 351 } 352 353 363 public final int readUnsignedByte() 364 throws IndexOutOfBoundsException { 365 366 int c = readFast(); 367 if (c < 0) { 368 throw new IndexOutOfBoundsException (); 369 } 370 return c; 371 } 372 373 383 public final int readUnsignedShort() 384 throws IndexOutOfBoundsException { 385 386 int c1 = readFast(); 387 int c2 = readFast(); 388 if ((c1 | c2) < 0) { 389 throw new IndexOutOfBoundsException (); 390 } 391 return ((c1 << 8) | c2); 392 } 393 394 396 406 public final long readUnsignedInt() 407 throws IndexOutOfBoundsException { 408 409 long c1 = readFast(); 410 long c2 = readFast(); 411 long c3 = readFast(); 412 long c4 = readFast(); 413 if ((c1 | c2 | c3 | c4) < 0) { 414 throw new IndexOutOfBoundsException (); 415 } 416 return ((c1 << 24) | (c2 << 16) | (c3 << 8) | c4); 417 } 418 419 423 private final long readUnsignedLong() 424 throws IndexOutOfBoundsException { 425 426 long c1 = readFast(); 427 long c2 = readFast(); 428 long c3 = readFast(); 429 long c4 = readFast(); 430 long c5 = readFast(); 431 long c6 = readFast(); 432 long c7 = readFast(); 433 long c8 = readFast(); 434 if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) < 0) { 435 throw new IndexOutOfBoundsException (); 436 } 437 return ((c1 << 56) | (c2 << 48) | (c3 << 40) | (c4 << 32) | 438 (c5 << 24) | (c6 << 16) | (c7 << 8) | c8); 439 } 440 441 454 public final String readBytes(int length) 455 throws IndexOutOfBoundsException { 456 457 StringBuffer buf = new StringBuffer (length); 458 for (int i = 0; i < length; i++) { 459 int c = readFast(); 460 if (c < 0) { 461 throw new IndexOutOfBoundsException (); 462 } 463 buf.append((char) c); 464 } 465 return buf.toString(); 466 } 467 468 480 public final String readChars(int length) 481 throws IndexOutOfBoundsException { 482 483 StringBuffer buf = new StringBuffer (length); 484 for (int i = 0; i < length; i++) { 485 buf.append(readChar()); 486 } 487 return buf.toString(); 488 } 489 490 502 public final void readBytes(char[] chars) 503 throws IndexOutOfBoundsException { 504 505 for (int i = 0; i < chars.length; i++) { 506 int c = readFast(); 507 if (c < 0) { 508 throw new IndexOutOfBoundsException (); 509 } 510 chars[i] = (char) c; 511 } 512 } 513 514 525 public final void readChars(char[] chars) 526 throws IndexOutOfBoundsException { 527 528 for (int i = 0; i < chars.length; i++) { 529 chars[i] = readChar(); 530 } 531 } 532 533 548 public final String readString(int length) 549 throws IndexOutOfBoundsException , IllegalArgumentException { 550 551 char[] chars = new char[length]; 552 readString(chars); 553 return new String (chars); 554 } 555 556 570 public final void readString(char[] chars) 571 throws IndexOutOfBoundsException , IllegalArgumentException { 572 573 off = UtfOps.bytesToChars(buf, off, chars, 0, chars.length, false); 574 } 575 576 586 public final int getStringByteLength() 587 throws IndexOutOfBoundsException , IllegalArgumentException { 588 589 if (available() >= 2 && 590 buf[off] == TupleOutput.NULL_STRING_UTF_VALUE && 591 buf[off + 1] == 0) { 592 return 2; 593 } else { 594 return UtfOps.getZeroTerminatedByteLength(buf, off) + 1; 595 } 596 } 597 598 604 public final int readPackedInt() { 605 606 int len = PackedInteger.getReadIntLength(buf, off); 607 int val = PackedInteger.readInt(buf, off); 608 609 off += len; 610 return val; 611 } 612 613 618 public final int getPackedIntByteLength() { 619 return PackedInteger.getReadIntLength(buf, off); 620 } 621 622 627 public final BigInteger readBigInteger() { 628 int len = readShort(); 629 if (len < 0) { 630 len = (- len); 631 } 632 byte[] a = new byte[len]; 633 a[0] = readByte(); 634 readFast(a, 1, a.length - 1); 635 return new BigInteger (a); 636 } 637 638 643 public final int getBigIntegerByteLength() { 644 int saveOff = off; 645 int len = readShort(); 646 off = saveOff; 647 if (len < 0) { 648 len = (- len); 649 } 650 return len + 2; 651 } 652 } 653 | Popular Tags |