Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2000, 2010 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.bind.tuple;
  
 
An InputStream with DataInput-like methods for reading tuple fields. It is used by TupleBinding.

This class has many methods that have the same signatures as methods in the java.io.DataInput interface. The reason this class does not implement java.io.DataInput is because it would break the interface contract for those methods because of data format differences.

Author(s):
Mark Hayes
See also:
Tuple Formats
 
 public class TupleInput extends FastInputStream {

    
Creates a tuple input object for reading a byte array of tuple data. A reference to the byte array will be kept by this object (it will not be copied) and therefore the byte array should not be modified while this object is in use.

Parameters:
buffer is the byte array to be read and should contain data in tuple format.
 
     public TupleInput(byte[] buffer) {
 
         super(buffer);
     }

    
Creates a tuple input object for reading a byte array of tuple data at a given offset for a given length. A reference to the byte array will be kept by this object (it will not be copied) and therefore the byte array should not be modified while this object is in use.

Parameters:
buffer is the byte array to be read and should contain data in tuple format.
offset is the byte offset at which to begin reading.
length is the number of bytes to be read.
 
     public TupleInput(byte[] bufferint offsetint length) {
 
         super(bufferoffsetlength);
     }

    
Creates a tuple input object from the data contained in a tuple output object. A reference to the tuple output's byte array will be kept by this object (it will not be copied) and therefore the tuple output object should not be modified while this object is in use.

Parameters:
output is the tuple output object containing the data to be read.
 
     public TupleInput(TupleOutput output) {
 
         super(output.getBufferBytes(), output.getBufferOffset(),
               output.getBufferLength());
     }
 
     // --- begin DataInput compatible methods ---
 
    
Reads a null-terminated UTF string from the data buffer and converts the data from UTF to Unicode. Reads values that were written using TupleOutput.writeString(java.lang.String).

Returns:
the converted string.
Throws:
java.lang.IndexOutOfBoundsException if no null terminating byte is found in the buffer.
java.lang.IllegalArgumentException malformed UTF data is encountered.
See also:
String Formats
 
     public final String readString()
         throws IndexOutOfBoundsExceptionIllegalArgumentException  {
 
         byte[] myBuf = ;
         int myOff = ;
        if (available() >= 2 &&
            myBuf[myOff] == . &&
            myBuf[myOff + 1] == 0) {
            skip(2);
            return null;
        } else {
            int byteLen = UtfOps.getZeroTerminatedByteLength(myBufmyOff);
            skip(byteLen + 1);
            return UtfOps.bytesToString(myBufmyOffbyteLen);
        }
    }

    
Reads a char (two byte) unsigned value from the buffer. Reads values that were written using TupleOutput.writeChar(int).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final char readChar()
        throws IndexOutOfBoundsException {
        return (charreadUnsignedShort();
    }

    
Reads a boolean (one byte) unsigned value from the buffer and returns true if it is non-zero and false if it is zero. Reads values that were written using TupleOutput.writeBoolean(boolean).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final boolean readBoolean()
        throws IndexOutOfBoundsException {
        int c = readFast();
        if (c < 0) {
            throw new IndexOutOfBoundsException();
        }
        return (c != 0);
    }

    
Reads a signed byte (one byte) value from the buffer. Reads values that were written using TupleOutput.writeByte(int).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final byte readByte()
        throws IndexOutOfBoundsException {
        return (byte) (readUnsignedByte() ^ 0x80);
    }

    
Reads a signed short (two byte) value from the buffer. Reads values that were written using TupleOutput.writeShort(int).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final short readShort()
        throws IndexOutOfBoundsException {
        return (short) (readUnsignedShort() ^ 0x8000);
    }

    
Reads a signed int (four byte) value from the buffer. Reads values that were written using TupleOutput.writeInt(int).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final int readInt()
        throws IndexOutOfBoundsException {
        return (int) (readUnsignedInt() ^ 0x80000000);
    }

    
Reads a signed long (eight byte) value from the buffer. Reads values that were written using TupleOutput.writeLong(long).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final long readLong()
        throws IndexOutOfBoundsException {
        return readUnsignedLong() ^ 0x8000000000000000L;
    }

    
Reads an unsorted float (four byte) value from the buffer. Reads values that were written using TupleOutput.writeFloat(float).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Floating Point Formats
    public final float readFloat()
        throws IndexOutOfBoundsException {
        return Float.intBitsToFloat((intreadUnsignedInt());
    }

    
Reads an unsorted double (eight byte) value from the buffer. Reads values that were written using TupleOutput.writeDouble(double).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Floating Point Formats
    public final double readDouble()
        throws IndexOutOfBoundsException {
        return Double.longBitsToDouble(readUnsignedLong());
    }

    
Reads a sorted float (four byte) value from the buffer. Reads values that were written using TupleOutput.writeSortedFloat(float).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Floating Point Formats
    public final float readSortedFloat()
        throws IndexOutOfBoundsException {
        int val = (intreadUnsignedInt();
        val ^= (val < 0) ? 0x80000000 : 0xffffffff;
        return Float.intBitsToFloat(val);
    }

    
Reads a sorted double (eight byte) value from the buffer. Reads values that were written using TupleOutput.writeSortedDouble(double).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Floating Point Formats
    public final double readSortedDouble()
        throws IndexOutOfBoundsException {
        long val = readUnsignedLong();
        val ^= (val < 0) ? 0x8000000000000000L : 0xffffffffffffffffL;
        return Double.longBitsToDouble(val);
    }

    
Reads an unsigned byte (one byte) value from the buffer. Reads values that were written using TupleOutput.writeUnsignedByte(int).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final int readUnsignedByte()
        throws IndexOutOfBoundsException {
        int c = readFast();
        if (c < 0) {
            throw new IndexOutOfBoundsException();
        }
        return c;
    }

    
Reads an unsigned short (two byte) value from the buffer. Reads values that were written using TupleOutput.writeUnsignedShort(int).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final int readUnsignedShort()
        throws IndexOutOfBoundsException {
        int c1 = readFast();
        int c2 = readFast();
        if ((c1 | c2) < 0) {
             throw new IndexOutOfBoundsException();
        }
        return ((c1 << 8) | c2);
    }
    // --- end DataInput compatible methods ---

    
Reads an unsigned int (four byte) value from the buffer. Reads values that were written using TupleOutput.writeUnsignedInt(long).

Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final long readUnsignedInt()
        throws IndexOutOfBoundsException {
        long c1 = readFast();
        long c2 = readFast();
        long c3 = readFast();
        long c4 = readFast();
        if ((c1 | c2 | c3 | c4) < 0) {
            throw new IndexOutOfBoundsException();
        }
        return ((c1 << 24) | (c2 << 16) | (c3 << 8) | c4);
    }

    
This method is private since an unsigned long cannot be treated as such in Java, nor converted to a BigInteger of the same value.
    private final long readUnsignedLong()
        throws IndexOutOfBoundsException {
        long c1 = readFast();
        long c2 = readFast();
        long c3 = readFast();
        long c4 = readFast();
        long c5 = readFast();
        long c6 = readFast();
        long c7 = readFast();
        long c8 = readFast();
        if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) < 0) {
             throw new IndexOutOfBoundsException();
        }
        return ((c1 << 56) | (c2 << 48) | (c3 << 40) | (c4 << 32) |
                (c5 << 24) | (c6 << 16) | (c7 << 8)  | c8);
    }

    
Reads the specified number of bytes from the buffer, converting each unsigned byte value to a character of the resulting string. Reads values that were written using TupleOutput.writeBytes(java.lang.String).

Parameters:
length is the number of bytes to be read.
Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final String readBytes(int length)
        throws IndexOutOfBoundsException {
        StringBuilder buf = new StringBuilder(length);
        for (int i = 0; i < lengthi++) {
            int c = readFast();
            if (c < 0) {
                throw new IndexOutOfBoundsException();
            }
            buf.append((charc);
        }
        return buf.toString();
    }

    
Reads the specified number of characters from the buffer, converting each two byte unsigned value to a character of the resulting string. Reads values that were written using TupleOutput.writeChars(java.lang.String).

Parameters:
length is the number of characters to be read.
Returns:
the value read from the buffer.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final String readChars(int length)
        throws IndexOutOfBoundsException {
        StringBuilder buf = new StringBuilder(length);
        for (int i = 0; i < lengthi++) {
            buf.append(readChar());
        }
        return buf.toString();
    }

    
Reads the specified number of bytes from the buffer, converting each unsigned byte value to a character of the resulting array. Reads values that were written using TupleOutput.writeBytes(java.lang.String).

Parameters:
chars is the array to receive the data and whose length is used to determine the number of bytes to be read.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final void readBytes(char[] chars)
        throws IndexOutOfBoundsException {
        for (int i = 0; i < chars.lengthi++) {
            int c = readFast();
            if (c < 0) {
                throw new IndexOutOfBoundsException();
            }
            chars[i] = (charc;
        }
    }

    
Reads the specified number of characters from the buffer, converting each two byte unsigned value to a character of the resulting array. Reads values that were written using TupleOutput.writeChars(java.lang.String).

Parameters:
chars is the array to receive the data and whose length is used to determine the number of characters to be read.
Throws:
java.lang.IndexOutOfBoundsException if not enough bytes are available in the buffer.
See also:
Integer Formats
    public final void readChars(char[] chars)
        throws IndexOutOfBoundsException {
        for (int i = 0; i < chars.lengthi++) {
            chars[i] = readChar();
        }
    }

    
Reads the specified number of UTF characters string from the data buffer and converts the data from UTF to Unicode. Reads values that were written using TupleOutput.writeString(char[]).

Parameters:
length is the number of characters to be read.
Returns:
the converted string.
Throws:
java.lang.IndexOutOfBoundsException if no null terminating byte is found in the buffer.
java.lang.IllegalArgumentException malformed UTF data is encountered.
See also:
String Formats
    public final String readString(int length)
        char[] chars = new char[length];
        readString(chars);
        return new String(chars);
    }

    
Reads the specified number of UTF characters string from the data buffer and converts the data from UTF to Unicode. Reads values that were written using TupleOutput.writeString(char[]).

Parameters:
chars is the array to receive the data and whose length is used to determine the number of characters to be read.
Throws:
java.lang.IndexOutOfBoundsException if no null terminating byte is found in the buffer.
java.lang.IllegalArgumentException malformed UTF data is encountered.
See also:
String Formats
    public final void readString(char[] chars)
         = UtfOps.bytesToChars(chars, 0, chars.lengthfalse);
    }

    
Returns the byte length of a null-terminated UTF string in the data buffer, including the terminator. Used with string values that were written using TupleOutput.writeString(java.lang.String).

Throws:
java.lang.IndexOutOfBoundsException if no null terminating byte is found in the buffer.
java.lang.IllegalArgumentException malformed UTF data is encountered.
See also:
String Formats
    public final int getStringByteLength()
        if (available() >= 2 &&
            [] == . &&
            [ + 1] == 0) {
            return 2;
        } else {
            return UtfOps.getZeroTerminatedByteLength() + 1;
        }
    }

    
Reads an unsorted packed integer.

See also:
Integer Formats
    public final int readPackedInt() {
        int len = PackedInteger.getReadIntLength();
        int val = PackedInteger.readInt();
         += len;
        return val;
    }

    
Returns the byte length of a packed integer.

See also:
Integer Formats
    public final int getPackedIntByteLength() {
        return PackedInteger.getReadIntLength();
    }

    
Reads an unsorted packed long integer.

See also:
Integer Formats
    public final long readPackedLong() {
        int len = PackedInteger.getReadLongLength();
        long val = PackedInteger.readLong();
         += len;
        return val;
    }

    
Returns the byte length of a packed long integer.

See also:
Integer Formats
    public final int getPackedLongByteLength() {
        return PackedInteger.getReadLongLength();
    }

    
Reads a sorted packed integer.

See also:
Integer Formats
    public final int readSortedPackedInt() {
        int len = PackedInteger.getReadSortedIntLength();
        int val = PackedInteger.readSortedInt();
         += len;
        return val;
    }

    
Returns the byte length of a sorted packed integer.

See also:
Integer Formats
    public final int getSortedPackedIntByteLength() {
        return PackedInteger.getReadSortedIntLength();
    }

    
Reads a sorted packed long integer.

See also:
Integer Formats
    public final long readSortedPackedLong() {
        int len = PackedInteger.getReadSortedLongLength();
        long val = PackedInteger.readSortedLong();
         += len;
        return val;
    }

    
Returns the byte length of a sorted packed long integer.

See also:
Integer Formats
    public final int getSortedPackedLongByteLength() {
        return PackedInteger.getReadSortedLongLength();
    }

    
Reads a BigInteger.

See also:
Integer Formats
    public final BigInteger readBigInteger() {
    
        int len = readShort();
        if (len < 0) {
            len = (- len);
        }
        byte[] a = new byte[len];
        a[0] = readByte();
        readFast(a, 1, a.length - 1);
        return new BigInteger(a);
    }

    
Returns the byte length of a BigInteger.

See also:
Integer Formats
    public final int getBigIntegerByteLength() {
    
        int saveOff = ;
        int len = readShort();
         = saveOff;
        if (len < 0) {
            len = (- len);
        }
        return len + 2;
    }
    
    
Reads an unsorted BigDecimal.

See also:
BigDecimal Formats
    public final BigDecimal readBigDecimal() {
    
        int scale = readPackedInt();
        int len = readPackedInt();
        byte[] a = new byte[len];
        readFast(a, 0, len);
        BigInteger unscaledVal = new BigInteger(a);
        return new BigDecimal(unscaledValscale);
    }
    
    
Returns the byte length of an unsorted BigDecimal.

See also:
BigDecimal Formats
    public final int getBigDecimalByteLength() {
    
        /* First get the length of the scale. */
        int scaleLen = getPackedIntByteLength();
        int saveOff = ;
         += scaleLen;
        
        /* 
         * Then get the length of the value which store the length of the 
         * following bytes. 
         */
        int lenOfUnscaleValLen = getPackedIntByteLength();
        
        /* Finally get the length of the following bytes. */
        int unscaledValLen = readPackedInt();
         = saveOff;
        return scaleLen + lenOfUnscaleValLen + unscaledValLen;
    }
    
    
Reads a sorted BigDecimal, with support for correct default sorting.

See also:
BigDecimal Formats
    public final BigDecimal readSortedBigDecimal() {    
        /* Get the sign of the BigDecimal. */
        int sign = readByte();
        
        /* Get the exponent of the BigDecimal. */
        int exponent = readSortedPackedInt();
        
        /*Get the normalized BigDecimal. */
        BigDecimal normalizedVal = readSortedNormalizedBigDecimal();
        
        /* 
         * After getting the normalized BigDecimal, we need to scale the value
         * with the exponent.
         */
        return normalizedVal.scaleByPowerOfTen(exponent * sign);
    }
    
    
Reads a sorted BigDecimal in normalized format with a single digit to the left of the decimal point.
    private final BigDecimal readSortedNormalizedBigDecimal() { 
        StringBuilder valStr = new StringBuilder(32);
        int subVal = readSortedPackedInt();
        int sign = subVal < 0 ? -1 : 1;
        
        /* Read through the buf, until we meet the terminator byte. */
        while (subVal != -1) {
        
            /* Adjust the sub-value back to the original. */
            subVal = subVal < 0 ? subVal + 1 : subVal;
            String groupDigits = Integer.toString(Math.abs(subVal));
        
            /* 
             * subVal < 100000000 means some leading zeros have been removed,
             * we have to add them back.
             */ 
            if (groupDigits.length() < 9) {
                final int insertLen = 9 - groupDigits.length();
                for (int i = 0; i < insertLeni++) {
                    valStr.append("0");
                }
            }
            valStr.append(groupDigits);
            subVal = readSortedPackedInt();
        }
        BigInteger digitsVal = new BigInteger(valStr.toString());
        if (sign < 0) {
            digitsVal = digitsVal.negate();
        }
        /* The normalized decimal has 1 digits in the int part. */
        int scale = valStr.length() - 1;
        
        /* 
         * Since we may pad trailing zeros for serialization, when doing 
         * de-serialization, we need to delete the trailing zeros. 
         */
        return new BigDecimal(digitsValscale).stripTrailingZeros();
    }
    
    
Returns the byte length of a sorted BigDecimal.

See also:
BigDecimal Formats
    public final int getSortedBigDecimalByteLength() {
    
        /* Save the original position, and read past the sigh byte. */
        int saveOff = ++;
        /* Get the length of the exponent. */
        int len = getSortedPackedIntByteLength(); /* the exponent */
        /* Skip to the digit part. */
         += len;
        
        /* 
         * Travel through the following SortedPackedIntegers, until we meet the 
         * terminator byte. 
         */
        int subVal = readSortedPackedInt();
        while (subVal != -1) {
            subVal = readSortedPackedInt();
        }
        
        /* 
         * off is the value of end offset, while saveOff is the beginning 
         * offset.
         */
        len =  - saveOff;
         = saveOff;
        return len;
    }
New to GrepCode? Check out our FAQ X