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 OutputStream with DataOutput-like methods for writing tuple fields. It is used by TupleBinding.

This class has many methods that have the same signatures as methods in the java.io.DataOutput interface. The reason this class does not implement java.io.DataOutput 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 TupleOutput extends FastOutputStream {

    
We represent a null string as a single FF UTF character, which cannot occur in a UTF encoded string.
 
     static final int NULL_STRING_UTF_VALUE = ((byte) 0xFF);

    
Creates a tuple output object for writing a byte array of tuple data.
 
     public TupleOutput() {
 
         super();
     }

    
Creates a tuple output object for writing a byte array of tuple data, using a given buffer. A new buffer will be allocated only if the number of bytes needed is greater than the length of this buffer. A reference to the byte array will be kept by this object and therefore the byte array should not be modified while this object is in use.

Parameters:
buffer is the byte array to use as the buffer.
 
     public TupleOutput(byte[] buffer) {
 
         super(buffer);
     }
 
     // --- begin DataOutput compatible methods ---
 
    
Writes the specified bytes to the buffer, converting each character to an unsigned byte value. Writes values that can be read using TupleInput.readBytes(int).

Parameters:
val is the string containing the values to be written. Only characters with values below 0x100 may be written using this method, since the high-order 8 bits of all characters are discarded.
Returns:
this tuple output object.
Throws:
java.lang.NullPointerException if the val parameter is null.
See also:
Integer Formats
 
     public final TupleOutput writeBytes(String val) {
 
         writeBytes(val.toCharArray());
         return this;
     }

    
Writes the specified characters to the buffer, converting each character to a two byte unsigned value. Writes values that can be read using TupleInput.readChars(int).

Parameters:
val is the string containing the characters to be written.
Returns:
this tuple output object.
Throws:
java.lang.NullPointerException if the val parameter is null.
See also:
Integer Formats
 
     public final TupleOutput writeChars(String val) {
 
         writeChars(val.toCharArray());
         return this;
    }

    
Writes the specified characters to the buffer, converting each character to UTF format, and adding a null terminator byte. Writes values that can be read using TupleInput.readString().

Parameters:
val is the string containing the characters to be written.
Returns:
this tuple output object.
See also:
String Formats
    public final TupleOutput writeString(String val) {
        if (val != null) {
            writeString(val.toCharArray());
        } else {
            writeFast();
        }
        writeFast(0);
        return this;
    }

    
Writes a char (two byte) unsigned value to the buffer. Writes values that can be read using TupleInput.readChar().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeChar(int val) {
        writeFast((byte) (val >>> 8));
        writeFast((byteval);
        return this;
    }

    
Writes a boolean (one byte) unsigned value to the buffer, writing one if the value is true and zero if it is false. Writes values that can be read using TupleInput.readBoolean().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeBoolean(boolean val) {
        writeFast(val ? (byte)1 : (byte)0);
        return this;
    }

    
Writes an signed byte (one byte) value to the buffer. Writes values that can be read using TupleInput.readByte().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeByte(int val) {
        writeUnsignedByte(val ^ 0x80);
        return this;
    }

    
Writes an signed short (two byte) value to the buffer. Writes values that can be read using TupleInput.readShort().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeShort(int val) {
        writeUnsignedShort(val ^ 0x8000);
        return this;
    }

    
Writes an signed int (four byte) value to the buffer. Writes values that can be read using TupleInput.readInt().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeInt(int val) {
        writeUnsignedInt(val ^ 0x80000000);
        return this;
    }

    
Writes an signed long (eight byte) value to the buffer. Writes values that can be read using TupleInput.readLong().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeLong(long val) {
        writeUnsignedLong(val ^ 0x8000000000000000L);
        return this;
    }

    
Writes an unsorted float (four byte) value to the buffer. Writes values that can be read using TupleInput.readFloat().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Floating Point Formats
    public final TupleOutput writeFloat(float val) {
        writeUnsignedInt(Float.floatToIntBits(val));
        return this;
    }

    
Writes an unsorted double (eight byte) value to the buffer. Writes values that can be read using TupleInput.readDouble().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Floating Point Formats
    public final TupleOutput writeDouble(double val) {
        writeUnsignedLong(Double.doubleToLongBits(val));
        return this;
    }

    
Writes a sorted float (four byte) value to the buffer. Writes values that can be read using TupleInput.readSortedFloat().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Floating Point Formats
    public final TupleOutput writeSortedFloat(float val) {
        int intVal = Float.floatToIntBits(val);
        intVal ^= (intVal < 0) ? 0xffffffff : 0x80000000;
        writeUnsignedInt(intVal);
        return this;
    }

    
Writes a sorted double (eight byte) value to the buffer. Writes values that can be read using TupleInput.readSortedDouble().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Floating Point Formats
    public final TupleOutput writeSortedDouble(double val) {
        long longVal = Double.doubleToLongBits(val);
        longVal ^= (longVal < 0) ? 0xffffffffffffffffL : 0x8000000000000000L;
        writeUnsignedLong(longVal);
        return this;
    }
    // --- end DataOutput compatible methods ---

    
Writes the specified bytes to the buffer, converting each character to an unsigned byte value. Writes values that can be read using TupleInput.readBytes(int).

Parameters:
chars is the array of values to be written. Only characters with values below 0x100 may be written using this method, since the high-order 8 bits of all characters are discarded.
Returns:
this tuple output object.
Throws:
java.lang.NullPointerException if the chars parameter is null.
See also:
Integer Formats
    public final TupleOutput writeBytes(char[] chars) {
        for (int i = 0; i < chars.lengthi++) {
            writeFast((bytechars[i]);
        }
        return this;
    }

    
Writes the specified characters to the buffer, converting each character to a two byte unsigned value. Writes values that can be read using TupleInput.readChars(int).

Parameters:
chars is the array of characters to be written.
Returns:
this tuple output object.
Throws:
java.lang.NullPointerException if the chars parameter is null.
See also:
Integer Formats
    public final TupleOutput writeChars(char[] chars) {
        for (int i = 0; i < chars.lengthi++) {
            writeFast((byte) (chars[i] >>> 8));
            writeFast((bytechars[i]);
        }
        return this;
    }

    
Writes the specified characters to the buffer, converting each character to UTF format. Writes values that can be read using TupleInput.readString(int) or TupleInput.readString(char[]).

Parameters:
chars is the array of characters to be written.
Returns:
this tuple output object.
Throws:
java.lang.NullPointerException if the chars parameter is null.
See also:
String Formats
    public final TupleOutput writeString(char[] chars) {
        if (chars.length == 0) return this;
        int utfLength = UtfOps.getByteLength(chars);
        makeSpace(utfLength);
        UtfOps.charsToBytes(chars, 0, getBufferBytes(), getBufferLength(),
                            chars.length);
        addSize(utfLength);
        return this;
    }

    
Writes an unsigned byte (one byte) value to the buffer. Writes values that can be read using TupleInput.readUnsignedByte().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeUnsignedByte(int val) {
        writeFast(val);
        return this;
    }

    
Writes an unsigned short (two byte) value to the buffer. Writes values that can be read using TupleInput.readUnsignedShort().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeUnsignedShort(int val) {
        writeFast((byte) (val >>> 8));
        writeFast((byteval);
        return this;
    }

    
Writes an unsigned int (four byte) value to the buffer. Writes values that can be read using TupleInput.readUnsignedInt().

Parameters:
val is the value to write to the buffer.
Returns:
this tuple output object.
See also:
Integer Formats
    public final TupleOutput writeUnsignedInt(long val) {
        writeFast((byte) (val >>> 24));
        writeFast((byte) (val >>> 16));
        writeFast((byte) (val >>> 8));
        writeFast((byteval);
        return this;
    }

    
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 TupleOutput writeUnsignedLong(long val) {
        writeFast((byte) (val >>> 56));
        writeFast((byte) (val >>> 48));
        writeFast((byte) (val >>> 40));
        writeFast((byte) (val >>> 32));
        writeFast((byte) (val >>> 24));
        writeFast((byte) (val >>> 16));
        writeFast((byte) (val >>> 8));
        writeFast((byteval);
        return this;
    }

    
Writes an unsorted packed integer.

See also:
Integer Formats
    public final TupleOutput writePackedInt(int val) {
        int oldLen = getBufferLength();
        int newLen = PackedInteger.writeInt(getBufferBytes(), oldLenval);
        addSize(newLen - oldLen);
        return this;
    }

    
Writes an unsorted packed long integer.

See also:
Integer Formats
    public final TupleOutput writePackedLong(long val) {
        int oldLen = getBufferLength();
        int newLen = PackedInteger.writeLong(getBufferBytes(), oldLenval);
        addSize(newLen - oldLen);
        return this;
    }

    
Writes a sorted packed integer.

See also:
Integer Formats
    public final TupleOutput writeSortedPackedInt(int val) {
        int oldLen = getBufferLength();
        int newLen = PackedInteger.writeSortedInt(getBufferBytes(), oldLen,
                                                  val);
        addSize(newLen - oldLen);
        return this;
    }

    
Writes a sorted packed long integer.

See also:
Integer Formats
    public final TupleOutput writeSortedPackedLong(long val) {
        int oldLen = getBufferLength();
        int newLen = PackedInteger.writeSortedLong(getBufferBytes(), oldLen,
                                                   val);
        addSize(newLen - oldLen);
        return this;
    }

    
Writes a BigInteger.

Throws:
java.lang.NullPointerException if val is null.
java.lang.IllegalArgumentException if the byte array representation of val is larger than 0x7fff bytes.
See also:
Integer Formats
    public final TupleOutput writeBigInteger(BigInteger val) {
    
        byte[] a = val.toByteArray();
        if (a.length > .) {
            throw new IllegalArgumentException
                ("BigInteger byte array is larger than 0x7fff bytes");
        }
        int firstByte = a[0];
        writeShort((firstByte < 0) ? (- a.length) : a.length);
        writeByte(firstByte);
        writeFast(a, 1, a.length - 1);
        return this;
    }

    
Returns the exact byte length that would would be output for a given BigInteger value if writeBigInteger(java.math.BigInteger) were called.

See also:
Integer Formats
    public static int getBigIntegerByteLength(BigInteger val) {
        return 2 /* length bytes */ +
               (val.bitLength() + 1 /* sign bit */ + 7 /* round up */) / 8;
    }
    
    
Writes an unsorted BigDecimal.

Throws:
java.lang.NullPointerException if val is null.
See also:
BigDecimal Formats
    public final TupleOutput writeBigDecimal(BigDecimal val) {
    
        /*
         * The byte format for a BigDecimal value is:
         *     Byte 0 ~ L:   The scale part written as a PackedInteger.
         *     Byte L+1 ~ M: The length of the unscaled value written as a
         *                   PackedInteger.
         *     Byte M+1 ~ N: The BigDecimal.toByteArray array, written 
         *                   without modification.
         *
         * Get the scale and the unscaled value of this BigDecimal.
         */
        int scale = val.scale();
        BigInteger unscaledVal = val.unscaledValue();
        
        /* Store the scale. */
        writePackedInt(scale);
        byte[] a = unscaledVal.toByteArray();
        int len = a.length;
        
        /* Store the length of the following bytes. */
        writePackedInt(len);
        
        /* Store the bytes of the BigDecimal, without modification. */
        writeFast(a, 0, len);
        return this;
    }
    
    
Returns the maximum byte length that would be output for a given BigDecimal value if writeBigDecimal(java.math.BigDecimal) were called.

See also:
BigDecimal Formats
    public static int getBigDecimalMaxByteLength(BigDecimal val) {
        BigInteger unscaledVal = val.unscaledValue();
        return . * 2 + 
               unscaledVal.toByteArray().length;
    }
    
    
Writes a sorted BigDecimal.

See also:
BigDecimal Formats
    public final TupleOutput writeSortedBigDecimal(BigDecimal val) {
        /* 
         * We have several options for the serialization of sorted BigDecimal.
         * The reason for choosing this method is that it is simpler and more
         * compact, and in some cases, comparison time will be less.  For other
         * methods and detailed discussion, please refer to [#18379].
         * 
         * First, we need to do the normalization, which means we normalize a
         * given BigDecimal into two parts: decimal part and the exponent part.
         * The decimal part contains one integer (non zero). For example,
         *      1234.56 will be normalized to 1.23456E3;
         *      123.4E100 will be normalized to 1.234E102;
         *      -123.4E-100 will be normalized to -1.234E-98.
         *
         * After the normalization, the byte format is:
         *     Byte 0: sign (-1 represents negative, 0 represents zero, and 1 
         *             represents positive).
         *     Byte 1 ~ 5: the exponent with sign, and written as a 
         *                 SortedPackedInteger value.
         *     Byte 6 ~ N: the normalized decimal part with sign.
         *
         * Get the scale and the unscaled value of this BigDecimal..
         */
        BigDecimal valNoTrailZeros = val.stripTrailingZeros();
        int scale = valNoTrailZeros.scale();
        BigInteger unscaledVal = valNoTrailZeros.unscaledValue();
        int sign = valNoTrailZeros.signum();
        
        /* Then do the normalization. */
        String unscaledValStr = unscaledVal.abs().toString();
        int normalizedScale = unscaledValStr.length() - 1;
        BigDecimal normalizedVal = new BigDecimal(unscaledVal
                                                  normalizedScale);
        int exponent = (normalizedScale - scale) * sign;
        
        /* Start serializing each part. */
        writeByte(sign);
        writeSortedPackedInt(exponent);
        writeSortedNormalizedBigDecimal(normalizedVal);
        return this;
    }
    
    
Writes a normalized BigDecimal.
        /*
         * The byte format for a sorted normalized {@code BigDecimal} value is:
         *     Byte 0 ~ N: Store all digits with sign. Each 9 digits is 
         *                 regarded as one integer, and written as a
         *                 SortedPackedInteger value.  If there are not enough
         *                 9 digits, pad trailing zeros. Since we may pad
         *                 trailing zeros for serialization, when doing
         *                 de-serialization, we need to delete the trailing
         *                 zeros. In order to designate a special value as the
         *                 terminator byte, we set
         *                     val = (val < 0) ? (val - 1) : val.
         *     Byte N + 1: Terminator byte. The terminator byte is -1, and
         *                 written as a SortedPackedInteger value.
         */
        /* get the precision, scale and sign of the BigDecimal. */
        int precision = val.precision();
        int scale = val.scale();
        int sign = val.signum();
        
        /* Start the serialization of the whole digits. */
        String digitsStr = val.abs().toPlainString();
        
        /* 
         * The default capacity of a StringBuilder is 16 chars, which is 
         * enough to hold a group of digits having 9 digits.
         */
        StringBuilder groupDigits = new StringBuilder();
        for (int i = 0; i < digitsStr.length();) {
            char digit = digitsStr.charAt(i++);
            
            /* Ignore the decimal. */
            if (digit != '.') {
                groupDigits.append(digit);
            }
            
            /* 
             * For the last group of the digits, if there are not 9 digits, pad
             * trailing zeros.
             */
            if (i == digitsStr.length() && groupDigits.length() < 9) {
                final int insertLen = 9 - groupDigits.length();
                for (int k = 0; k < insertLenk++) {
                    groupDigits.append("0");
                }
            }
            
            /* Group every 9 digits as an Integer. */            
            if (groupDigits.length() == 9) {
                int subVal = Integer.valueOf(groupDigits.toString());
                if (sign < 0) {
                    subVal = -subVal;
                }
                
                /* 
                 * Reset the sub-value, so the value -1 will be designated as
                 * the terminator byte.
                 */
                subVal = subVal < 0 ? subVal - 1 : subVal;
                writeSortedPackedInt(subVal);
                groupDigits.setLength(0);
            }
        }
        
        /* Write the terminator byte. */
        writeSortedPackedInt(-1);
        return this;
    }
    
    
Returns the maximum byte length that would be output for a given BigDecimal value if writeSortedBigDecimal(java.math.BigDecimal) were called.

See also:
BigDecimal Formats
    public static int getSortedBigDecimalMaxByteLength(BigDecimal val) {
    
        String digitsStr = val.stripTrailingZeros().unscaledValue().abs().
                           toString();
        
        int numOfGroups = (digitsStr.length() + 8 /* round up */) / 9;
        
        return 1 /* sign */ + 
               . /* exponent */ +
               . * numOfGroups /* all the digits */ +
               1; /* terminator byte */
    }
New to GrepCode? Check out our FAQ X