1 8 9 package com.sleepycat.bind.tuple; 10 11 import java.math.BigInteger ; 12 13 import com.sleepycat.util.FastOutputStream; 14 import com.sleepycat.util.PackedInteger; 15 import com.sleepycat.util.UtfOps; 16 17 76 public class TupleOutput extends FastOutputStream { 77 78 82 static final int NULL_STRING_UTF_VALUE = ((byte) 0xFF); 83 84 87 public TupleOutput() { 88 89 super(); 90 } 91 92 101 public TupleOutput(byte[] buffer) { 102 103 super(buffer); 104 } 105 106 108 121 public final TupleOutput writeBytes(String val) { 122 123 writeBytes(val.toCharArray()); 124 return this; 125 } 126 127 138 public final TupleOutput writeChars(String val) { 139 140 writeChars(val.toCharArray()); 141 return this; 142 } 143 144 155 public final TupleOutput writeString(String val) { 156 157 if (val != null) { 158 writeString(val.toCharArray()); 159 } else { 160 writeFast(NULL_STRING_UTF_VALUE); 161 } 162 writeFast(0); 163 return this; 164 } 165 166 174 public final TupleOutput writeChar(int val) { 175 176 writeFast((byte) (val >>> 8)); 177 writeFast((byte) val); 178 return this; 179 } 180 181 190 public final TupleOutput writeBoolean(boolean val) { 191 192 writeFast(val ? (byte)1 : (byte)0); 193 return this; 194 } 195 196 204 public final TupleOutput writeByte(int val) { 205 206 writeUnsignedByte(val ^ 0x80); 207 return this; 208 } 209 210 218 public final TupleOutput writeShort(int val) { 219 220 writeUnsignedShort(val ^ 0x8000); 221 return this; 222 } 223 224 232 public final TupleOutput writeInt(int val) { 233 234 writeUnsignedInt(val ^ 0x80000000); 235 return this; 236 } 237 238 246 public final TupleOutput writeLong(long val) { 247 248 writeUnsignedLong(val ^ 0x8000000000000000L); 249 return this; 250 } 251 252 268 public final TupleOutput writeFloat(float val) { 269 270 writeUnsignedInt(Float.floatToIntBits(val)); 271 return this; 272 } 273 274 290 public final TupleOutput writeDouble(double val) { 291 292 writeUnsignedLong(Double.doubleToLongBits(val)); 293 return this; 294 } 295 296 313 public final TupleOutput writeSortedFloat(float val) { 314 315 int intVal = Float.floatToIntBits(val); 316 intVal ^= (intVal < 0) ? 0xffffffff : 0x80000000; 317 writeUnsignedInt(intVal); 318 return this; 319 } 320 321 338 public final TupleOutput writeSortedDouble(double val) { 339 340 long longVal = Double.doubleToLongBits(val); 341 longVal ^= (longVal < 0) ? 0xffffffffffffffffL : 0x8000000000000000L; 342 writeUnsignedLong(longVal); 343 return this; 344 } 345 346 348 361 public final TupleOutput writeBytes(char[] chars) { 362 363 for (int i = 0; i < chars.length; i++) { 364 writeFast((byte) chars[i]); 365 } 366 return this; 367 } 368 369 380 public final TupleOutput writeChars(char[] chars) { 381 382 for (int i = 0; i < chars.length; i++) { 383 writeFast((byte) (chars[i] >>> 8)); 384 writeFast((byte) chars[i]); 385 } 386 return this; 387 } 388 389 402 public final TupleOutput writeString(char[] chars) { 403 404 if (chars.length == 0) return this; 405 406 int utfLength = UtfOps.getByteLength(chars); 407 408 makeSpace(utfLength); 409 UtfOps.charsToBytes(chars, 0, getBufferBytes(), getBufferLength(), 410 chars.length); 411 addSize(utfLength); 412 return this; 413 } 414 415 424 public final TupleOutput writeUnsignedByte(int val) { 425 426 writeFast(val); 427 return this; 428 } 429 430 439 public final TupleOutput writeUnsignedShort(int val) { 440 441 writeFast((byte) (val >>> 8)); 442 writeFast((byte) val); 443 return this; 444 } 445 446 455 public final TupleOutput writeUnsignedInt(long val) { 456 457 writeFast((byte) (val >>> 24)); 458 writeFast((byte) (val >>> 16)); 459 writeFast((byte) (val >>> 8)); 460 writeFast((byte) val); 461 return this; 462 } 463 464 468 private final TupleOutput writeUnsignedLong(long val) { 469 470 writeFast((byte) (val >>> 56)); 471 writeFast((byte) (val >>> 48)); 472 writeFast((byte) (val >>> 40)); 473 writeFast((byte) (val >>> 32)); 474 writeFast((byte) (val >>> 24)); 475 writeFast((byte) (val >>> 16)); 476 writeFast((byte) (val >>> 8)); 477 writeFast((byte) val); 478 return this; 479 } 480 481 487 public final void writePackedInt(int val) { 488 489 makeSpace(PackedInteger.MAX_LENGTH); 490 491 int oldLen = getBufferLength(); 492 int newLen = PackedInteger.writeInt(getBufferBytes(), oldLen, val); 493 494 addSize(newLen - oldLen); 495 } 496 497 522 public final TupleOutput writeBigInteger(BigInteger val) { 523 byte[] a = val.toByteArray(); 524 if (a.length > Short.MAX_VALUE) { 525 throw new IllegalArgumentException 526 ("BigInteger byte array is larger than 0x7fff bytes"); 527 } 528 int firstByte = a[0]; 529 writeShort((firstByte < 0) ? (- a.length) : a.length); 530 writeByte(firstByte); 531 writeFast(a, 1, a.length - 1); 532 return this; 533 } 534 535 540 public static int getBigIntegerByteLength(BigInteger val) { 541 return 2 + 542 (val.bitLength() + 1 + 7 ) / 8; 543 } 544 } 545 | Popular Tags |