com.amazon.carbonado.raw
Class DataEncoder

java.lang.Object
  extended by com.amazon.carbonado.raw.DataEncoder

public class DataEncoder
extends Object

A very low-level class that supports encoding of primitive data. For encoding data into keys, see KeyEncoder.

Author:
Brian S O'Neill
See Also:
DataDecoder

Constructor Summary
DataEncoder()
           
 
Method Summary
static int calculateEncodedLength(BigDecimal value)
          Returns the amount of bytes required to encode the given BigDecimal.
static int calculateEncodedLength(BigInteger value)
          Returns the amount of bytes required to encode the given BigInteger.
static int calculateEncodedLength(byte[] value)
          Returns the amount of bytes required to encode the given byte array.
static int calculateEncodedLength(byte[] value, int valueOffset, int valueLength)
          Returns the amount of bytes required to encode the given byte array.
static int calculateEncodedStringLength(String value)
          Returns the amount of bytes required to encode the given String.
static int encode(BigDecimal value, byte[] dst, int dstOffset)
          Encodes the given optional BigDecimal into a variable amount of bytes.
static int encode(BigInteger value, byte[] dst, int dstOffset)
          Encodes the given optional BigInteger into a variable amount of bytes.
static void encode(boolean value, byte[] dst, int dstOffset)
          Encodes the given boolean into exactly 1 byte.
static void encode(Boolean value, byte[] dst, int dstOffset)
          Encodes the given Boolean object into exactly 1 byte.
static int encode(byte[] value, byte[] dst, int dstOffset)
          Encodes the given optional byte array into a variable amount of bytes.
static int encode(byte[] value, int valueOffset, int valueLength, byte[] dst, int dstOffset)
          Encodes the given optional byte array into a variable amount of bytes.
static void encode(byte value, byte[] dst, int dstOffset)
          Encodes the given signed byte into exactly 1 byte.
static int encode(Byte value, byte[] dst, int dstOffset)
          Encodes the given signed Byte object into exactly 1 or 2 bytes.
static int encode(Character value, byte[] dst, int dstOffset)
          Encodes the given Character object into exactly 1 or 3 bytes.
static void encode(char value, byte[] dst, int dstOffset)
          Encodes the given character into exactly 2 bytes.
static void encode(double value, byte[] dst, int dstOffset)
          Encodes the given double into exactly 8 bytes.
static void encode(Double value, byte[] dst, int dstOffset)
          Encodes the given Double object into exactly 8 bytes.
static void encode(float value, byte[] dst, int dstOffset)
          Encodes the given float into exactly 4 bytes.
static void encode(Float value, byte[] dst, int dstOffset)
          Encodes the given Float object into exactly 4 bytes.
static void encode(int value, byte[] dst, int dstOffset)
          Encodes the given signed integer into exactly 4 bytes.
static int encode(Integer value, byte[] dst, int dstOffset)
          Encodes the given signed Integer object into exactly 1 or 5 bytes.
static void encode(long value, byte[] dst, int dstOffset)
          Encodes the given signed long into exactly 8 bytes.
static int encode(Long value, byte[] dst, int dstOffset)
          Encodes the given signed Long object into exactly 1 or 9 bytes.
static void encode(short value, byte[] dst, int dstOffset)
          Encodes the given signed short into exactly 2 bytes.
static int encode(Short value, byte[] dst, int dstOffset)
          Encodes the given signed Short object into exactly 1 or 3 bytes.
static int encode(String value, byte[] dst, int dstOffset)
          Encodes the given optional String into a variable amount of bytes.
static byte[] encodeSingle(byte[] value, int prefixPadding, int suffixPadding)
          Encodes the given byte array for use when there is only a single property, whose type is a byte array.
static byte[] encodeSingleNullable(byte[] value)
          Encodes the given byte array for use when there is only a single nullable property, whose type is a byte array.
static byte[] encodeSingleNullable(byte[] value, int prefixPadding, int suffixPadding)
          Encodes the given byte array for use when there is only a single nullable property, whose type is a byte array.
static int writeLength(int valueLength, OutputStream out)
          Writes a positive length value in up to five bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataEncoder

public DataEncoder()
Method Detail

encode

public static void encode(int value,
                          byte[] dst,
                          int dstOffset)
Encodes the given signed integer into exactly 4 bytes.

Parameters:
value - signed integer value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static int encode(Integer value,
                         byte[] dst,
                         int dstOffset)
Encodes the given signed Integer object into exactly 1 or 5 bytes. If the Integer object is never expected to be null, consider encoding as an int primitive.

Parameters:
value - optional signed Integer value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

encode

public static void encode(long value,
                          byte[] dst,
                          int dstOffset)
Encodes the given signed long into exactly 8 bytes.

Parameters:
value - signed long value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static int encode(Long value,
                         byte[] dst,
                         int dstOffset)
Encodes the given signed Long object into exactly 1 or 9 bytes. If the Long object is never expected to be null, consider encoding as a long primitive.

Parameters:
value - optional signed Long value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

encode

public static void encode(byte value,
                          byte[] dst,
                          int dstOffset)
Encodes the given signed byte into exactly 1 byte.

Parameters:
value - signed byte value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static int encode(Byte value,
                         byte[] dst,
                         int dstOffset)
Encodes the given signed Byte object into exactly 1 or 2 bytes. If the Byte object is never expected to be null, consider encoding as a byte primitive.

Parameters:
value - optional signed Byte value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

encode

public static void encode(short value,
                          byte[] dst,
                          int dstOffset)
Encodes the given signed short into exactly 2 bytes.

Parameters:
value - signed short value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static int encode(Short value,
                         byte[] dst,
                         int dstOffset)
Encodes the given signed Short object into exactly 1 or 3 bytes. If the Short object is never expected to be null, consider encoding as a short primitive.

Parameters:
value - optional signed Short value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

encode

public static void encode(char value,
                          byte[] dst,
                          int dstOffset)
Encodes the given character into exactly 2 bytes.

Parameters:
value - character value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static int encode(Character value,
                         byte[] dst,
                         int dstOffset)
Encodes the given Character object into exactly 1 or 3 bytes. If the Character object is never expected to be null, consider encoding as a char primitive.

Parameters:
value - optional Character value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

encode

public static void encode(boolean value,
                          byte[] dst,
                          int dstOffset)
Encodes the given boolean into exactly 1 byte.

Parameters:
value - boolean value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static void encode(Boolean value,
                          byte[] dst,
                          int dstOffset)
Encodes the given Boolean object into exactly 1 byte.

Parameters:
value - optional Boolean value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static void encode(float value,
                          byte[] dst,
                          int dstOffset)
Encodes the given float into exactly 4 bytes.

Parameters:
value - float value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static void encode(Float value,
                          byte[] dst,
                          int dstOffset)
Encodes the given Float object into exactly 4 bytes. A non-canonical NaN value is used to represent null.

Parameters:
value - optional Float value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static void encode(double value,
                          byte[] dst,
                          int dstOffset)
Encodes the given double into exactly 8 bytes.

Parameters:
value - double value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static void encode(Double value,
                          byte[] dst,
                          int dstOffset)
Encodes the given Double object into exactly 8 bytes. A non-canonical NaN value is used to represent null.

Parameters:
value - optional Double value to encode
dst - destination for encoded bytes
dstOffset - offset into destination array

encode

public static int encode(BigInteger value,
                         byte[] dst,
                         int dstOffset)
Encodes the given optional BigInteger into a variable amount of bytes. If the BigInteger is null, exactly 1 byte is written. Otherwise, the amount written can be determined by calling calculateEncodedLength.

Parameters:
value - BigInteger value to encode, may be null
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written
Since:
1.2

calculateEncodedLength

public static int calculateEncodedLength(BigInteger value)
Returns the amount of bytes required to encode the given BigInteger.

Parameters:
value - BigInteger value to encode, may be null
Returns:
amount of bytes needed to encode
Since:
1.2

encode

public static int encode(BigDecimal value,
                         byte[] dst,
                         int dstOffset)
Encodes the given optional BigDecimal into a variable amount of bytes. If the BigDecimal is null, exactly 1 byte is written. Otherwise, the amount written can be determined by calling calculateEncodedLength.

Parameters:
value - BigDecimal value to encode, may be null
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written
Since:
1.2

calculateEncodedLength

public static int calculateEncodedLength(BigDecimal value)
Returns the amount of bytes required to encode the given BigDecimal.

Parameters:
value - BigDecimal value to encode, may be null
Returns:
amount of bytes needed to encode
Since:
1.2

encode

public static int encode(byte[] value,
                         byte[] dst,
                         int dstOffset)
Encodes the given optional byte array into a variable amount of bytes. If the byte array is null, exactly 1 byte is written. Otherwise, the amount written can be determined by calling calculateEncodedLength.

Parameters:
value - byte array value to encode, may be null
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

encode

public static int encode(byte[] value,
                         int valueOffset,
                         int valueLength,
                         byte[] dst,
                         int dstOffset)
Encodes the given optional byte array into a variable amount of bytes. If the byte array is null, exactly 1 byte is written. Otherwise, the amount written can be determined by calling calculateEncodedLength.

Parameters:
value - byte array value to encode, may be null
valueOffset - offset into byte array
valueLength - length of data in byte array
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

calculateEncodedLength

public static int calculateEncodedLength(byte[] value)
Returns the amount of bytes required to encode the given byte array.

Parameters:
value - byte array value to encode, may be null
Returns:
amount of bytes needed to encode

calculateEncodedLength

public static int calculateEncodedLength(byte[] value,
                                         int valueOffset,
                                         int valueLength)
Returns the amount of bytes required to encode the given byte array.

Parameters:
value - byte array value to encode, may be null
valueOffset - offset into byte array
valueLength - length of data in byte array
Returns:
amount of bytes needed to encode

encode

public static int encode(String value,
                         byte[] dst,
                         int dstOffset)
Encodes the given optional String into a variable amount of bytes. The amount written can be determined by calling calculateEncodedStringLength.

Strings are encoded in a fashion similar to UTF-8, in that ASCII characters are written in one byte. This encoding is more efficient than UTF-8, but it isn't compatible with UTF-8.

Parameters:
value - String value to encode, may be null
dst - destination for encoded bytes
dstOffset - offset into destination array
Returns:
amount of bytes written

calculateEncodedStringLength

public static int calculateEncodedStringLength(String value)
Returns the amount of bytes required to encode the given String.

Parameters:
value - String to encode, may be null

writeLength

public static int writeLength(int valueLength,
                              OutputStream out)
                       throws IOException
Writes a positive length value in up to five bytes.

Returns:
number of bytes written
Throws:
IOException
Since:
1.2

encodeSingle

public static byte[] encodeSingle(byte[] value,
                                  int prefixPadding,
                                  int suffixPadding)
Encodes the given byte array for use when there is only a single property, whose type is a byte array. The original byte array is returned if the padding lengths are zero.

Parameters:
prefixPadding - amount of extra bytes to allocate at start of encoded byte array
suffixPadding - amount of extra bytes to allocate at end of encoded byte array

encodeSingleNullable

public static byte[] encodeSingleNullable(byte[] value)
Encodes the given byte array for use when there is only a single nullable property, whose type is a byte array.


encodeSingleNullable

public static byte[] encodeSingleNullable(byte[] value,
                                          int prefixPadding,
                                          int suffixPadding)
Encodes the given byte array for use when there is only a single nullable property, whose type is a byte array.

Parameters:
prefixPadding - amount of extra bytes to allocate at start of encoded byte array
suffixPadding - amount of extra bytes to allocate at end of encoded byte array


Copyright © 2006-2009 Amazon Technologies, Inc.. All Rights Reserved.