Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TupleInput.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: TupleInput.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.FastInputStream;
00013 import com.sleepycat.util.UtfOps;
00014 
00056 public class TupleInput extends FastInputStream {
00057 
00067     public TupleInput(byte[] buffer) {
00068 
00069         super(buffer);
00070     }
00071 
00085     public TupleInput(byte[] buffer, int offset, int length) {
00086 
00087         super(buffer, offset, length);
00088     }
00089 
00098     public TupleInput(TupleOutput output) {
00099 
00100         super(output.getBufferBytes(), output.getBufferOffset(),
00101               output.getBufferLength());
00102     }
00103 
00104     // --- begin DataInput compatible methods ---
00105 
00119     public final String readString()
00120         throws IndexOutOfBoundsException, IllegalArgumentException  {
00121 
00122         byte[] buf = getBufferBytes();
00123         int off = getBufferOffset();
00124         if (available() >= 2 &&
00125             buf[off] == TupleOutput.NULL_STRING_UTF_VALUE &&
00126             buf[off + 1] == 0) {
00127             skip(2);
00128             return null;
00129         } else {
00130             int byteLen = UtfOps.getZeroTerminatedByteLength(buf, off);
00131             skip(byteLen + 1);
00132             return UtfOps.bytesToString(buf, off, byteLen);
00133         }
00134     }
00135 
00145     public final char readChar() throws IndexOutOfBoundsException {
00146 
00147         return (char) readUnsignedShort();
00148     }
00149 
00160     public final boolean readBoolean() throws IndexOutOfBoundsException {
00161 
00162         int c = readFast();
00163         if (c < 0) {
00164             throw new IndexOutOfBoundsException();
00165         }
00166         return (c != 0);
00167     }
00168 
00178     public final byte readByte() throws IndexOutOfBoundsException {
00179 
00180         return (byte) (readUnsignedByte() ^ 0x80);
00181     }
00182 
00192     public final short readShort() throws IndexOutOfBoundsException {
00193 
00194         return (short) (readUnsignedShort() ^ 0x8000);
00195     }
00196 
00206     public final int readInt() throws IndexOutOfBoundsException {
00207 
00208         return (int) (readUnsignedInt() ^ 0x80000000);
00209     }
00210 
00220     public final long readLong() throws IndexOutOfBoundsException {
00221 
00222         return readUnsignedLong() ^ 0x8000000000000000L;
00223     }
00224 
00236     public final float readFloat() throws IndexOutOfBoundsException {
00237 
00238         return Float.intBitsToFloat((int) readUnsignedInt());
00239     }
00240 
00252     public final double readDouble() throws IndexOutOfBoundsException {
00253 
00254         return Double.longBitsToDouble(readUnsignedLong());
00255     }
00256 
00267     public final int readUnsignedByte() throws IndexOutOfBoundsException {
00268 
00269         int c = readFast();
00270         if (c < 0) {
00271             throw new IndexOutOfBoundsException();
00272         }
00273         return c;
00274     }
00275 
00286     public final int readUnsignedShort() throws IndexOutOfBoundsException {
00287 
00288         int c1 = readFast();
00289         int c2 = readFast();
00290         if ((c1 | c2) < 0) {
00291              throw new IndexOutOfBoundsException();
00292         }
00293         return ((c1 << 8) | c2);
00294     }
00295 
00296     // --- end DataInput compatible methods ---
00297 
00308     public final long readUnsignedInt() throws IndexOutOfBoundsException {
00309 
00310         long c1 = readFast();
00311         long c2 = readFast();
00312         long c3 = readFast();
00313         long c4 = readFast();
00314         if ((c1 | c2 | c3 | c4) < 0) {
00315             throw new IndexOutOfBoundsException();
00316         }
00317         return ((c1 << 24) | (c2 << 16) | (c3 << 8) | c4);
00318     }
00319 
00324     private final long readUnsignedLong() throws IndexOutOfBoundsException {
00325 
00326         long c1 = readFast();
00327         long c2 = readFast();
00328         long c3 = readFast();
00329         long c4 = readFast();
00330         long c5 = readFast();
00331         long c6 = readFast();
00332         long c7 = readFast();
00333         long c8 = readFast();
00334         if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) < 0) {
00335              throw new IndexOutOfBoundsException();
00336         }
00337         return ((c1 << 56) | (c2 << 48) | (c3 << 40) | (c4 << 32) |
00338                 (c5 << 24) | (c6 << 16) | (c7 << 8)  | c8);
00339     }
00340 
00354     public final String readBytes(int length)
00355         throws IndexOutOfBoundsException {
00356 
00357         StringBuffer buf = new StringBuffer(length);
00358         for (int i = 0; i < length; i++) {
00359             int c = readFast();
00360             if (c < 0) {
00361                 throw new IndexOutOfBoundsException();
00362             }
00363             buf.append((char) c);
00364         }
00365         return buf.toString();
00366     }
00367 
00380     public final String readChars(int length)
00381         throws IndexOutOfBoundsException {
00382 
00383         StringBuffer buf = new StringBuffer(length);
00384         for (int i = 0; i < length; i++) {
00385             buf.append(readChar());
00386         }
00387         return buf.toString();
00388     }
00389 
00402     public final void readBytes(char[] chars)
00403         throws IndexOutOfBoundsException {
00404 
00405         for (int i = 0; i < chars.length; i++) {
00406             int c = readFast();
00407             if (c < 0) {
00408                 throw new IndexOutOfBoundsException();
00409             }
00410             chars[i] = (char) c;
00411         }
00412     }
00413 
00425     public final void readChars(char[] chars)
00426         throws IndexOutOfBoundsException {
00427 
00428         for (int i = 0; i < chars.length; i++) {
00429             chars[i] = readChar();
00430         }
00431     }
00432 
00448     public final String readString(int length)
00449         throws IndexOutOfBoundsException, IllegalArgumentException  {
00450 
00451         char[] chars = new char[length];
00452         readString(chars);
00453         return new String(chars);
00454     }
00455 
00470     public final void readString(char[] chars)
00471         throws IndexOutOfBoundsException, IllegalArgumentException  {
00472 
00473         byte[] buf = getBufferBytes();
00474         off = UtfOps.bytesToChars(buf, off, chars, 0, chars.length, false);
00475     }
00476 }

Generated on Sun Dec 25 12:14:30 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2