00001 /*- 00002 * See the file LICENSE for redistribution information. 00003 * 00004 * Copyright (c) 2000-2005 00005 * Sleepycat Software. All rights reserved. 00006 * 00007 * $Id: TupleOutput.java,v 12.1 2005/01/31 19:27:32 mark Exp $ 00008 */ 00009 00010 package com.sleepycat.bind.tuple; 00011 00012 import com.sleepycat.util.FastOutputStream; 00013 import com.sleepycat.util.UtfOps; 00014 00056 public class TupleOutput extends FastOutputStream { 00057 00062 static final int NULL_STRING_UTF_VALUE = ((byte) 0xFF); 00063 00067 public TupleOutput() { 00068 00069 super(); 00070 } 00071 00081 public TupleOutput(byte[] buffer) { 00082 00083 super(buffer); 00084 } 00085 00086 // --- begin DataOutput compatible methods --- 00087 00101 public final TupleOutput writeBytes(String val) { 00102 00103 writeBytes(val.toCharArray()); 00104 return this; 00105 } 00106 00118 public final TupleOutput writeChars(String val) { 00119 00120 writeChars(val.toCharArray()); 00121 return this; 00122 } 00123 00135 public final TupleOutput writeString(String val) { 00136 00137 if (val != null) { 00138 writeString(val.toCharArray()); 00139 } else { 00140 writeFast(NULL_STRING_UTF_VALUE); 00141 } 00142 writeFast(0); 00143 return this; 00144 } 00145 00154 public final TupleOutput writeChar(int val) { 00155 00156 writeFast((byte) (val >>> 8)); 00157 writeFast((byte) val); 00158 return this; 00159 } 00160 00170 public final TupleOutput writeBoolean(boolean val) { 00171 00172 writeFast(val ? (byte)1 : (byte)0); 00173 return this; 00174 } 00175 00184 public final TupleOutput writeByte(int val) { 00185 00186 writeUnsignedByte(val ^ 0x80); 00187 return this; 00188 } 00189 00198 public final TupleOutput writeShort(int val) { 00199 00200 writeUnsignedShort(val ^ 0x8000); 00201 return this; 00202 } 00203 00212 public final TupleOutput writeInt(int val) { 00213 00214 writeUnsignedInt(val ^ 0x80000000); 00215 return this; 00216 } 00217 00226 public final TupleOutput writeLong(long val) { 00227 00228 writeUnsignedLong(val ^ 0x8000000000000000L); 00229 return this; 00230 } 00231 00242 public final TupleOutput writeFloat(float val) { 00243 00244 writeUnsignedInt(Float.floatToIntBits(val)); 00245 return this; 00246 } 00247 00258 public final TupleOutput writeDouble(double val) { 00259 00260 writeUnsignedLong(Double.doubleToLongBits(val)); 00261 return this; 00262 } 00263 00264 // --- end DataOutput compatible methods --- 00265 00279 public final TupleOutput writeBytes(char[] chars) { 00280 00281 for (int i = 0; i < chars.length; i++) { 00282 writeFast((byte) chars[i]); 00283 } 00284 return this; 00285 } 00286 00298 public final TupleOutput writeChars(char[] chars) { 00299 00300 for (int i = 0; i < chars.length; i++) { 00301 writeFast((byte) (chars[i] >>> 8)); 00302 writeFast((byte) chars[i]); 00303 } 00304 return this; 00305 } 00306 00320 public final TupleOutput writeString(char[] chars) { 00321 00322 if (chars.length == 0) return this; 00323 00324 int utfLength = UtfOps.getByteLength(chars); 00325 00326 makeSpace(utfLength); 00327 UtfOps.charsToBytes(chars, 0, getBufferBytes(), getBufferLength(), 00328 chars.length); 00329 addSize(utfLength); 00330 return this; 00331 } 00332 00342 public final TupleOutput writeUnsignedByte(int val) { 00343 00344 writeFast(val); 00345 return this; 00346 } 00347 00357 public final TupleOutput writeUnsignedShort(int val) { 00358 00359 writeFast((byte) (val >>> 8)); 00360 writeFast((byte) val); 00361 return this; 00362 } 00363 00373 public final TupleOutput writeUnsignedInt(long val) { 00374 00375 writeFast((byte) (val >>> 24)); 00376 writeFast((byte) (val >>> 16)); 00377 writeFast((byte) (val >>> 8)); 00378 writeFast((byte) val); 00379 return this; 00380 } 00381 00386 private final TupleOutput writeUnsignedLong(long val) { 00387 00388 writeFast((byte) (val >>> 56)); 00389 writeFast((byte) (val >>> 48)); 00390 writeFast((byte) (val >>> 40)); 00391 writeFast((byte) (val >>> 32)); 00392 writeFast((byte) (val >>> 24)); 00393 writeFast((byte) (val >>> 16)); 00394 writeFast((byte) (val >>> 8)); 00395 writeFast((byte) val); 00396 return this; 00397 } 00398 }