org.scale7.cassandra.pelops
Class Bytes

java.lang.Object
  extended by org.scale7.cassandra.pelops.Bytes

public class Bytes
extends Object

Wraps a ByteBuffer and provides useful methods to operate on it. Also provides numerous factory methods to create instances based on various data types.

In an effort to provide a very stable and well tested marshalling strategy this class uses the various methods available on ByteBuffer to perform serialization. The exceptions to this are the UUID and String methods (see their javadoc comments for details).

Note: Instances of this class should *not* be considered thread safe.


Field Summary
static Bytes EMPTY
           
static Bytes NULL
           
 
Constructor Summary
Bytes(byte[] bytes)
          Constructs a new instance based on the provided byte array.
Bytes(ByteBuffer bytes)
          Constructs a new instance based on the provided byte buffer.
 
Method Summary
 Bytes duplicate()
          Returns a duplicate of the bytes instance.
 boolean equals(Object o)
          Determines if two instances of this class are equals using the Arrays.equals(byte[], byte[]) method.
static Bytes fromBoolean(boolean value)
          Creates an instance based on the provided value.
static Bytes fromBoolean(Boolean value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromByte(byte value)
          Creates an instance based on the provided value.
static Bytes fromByte(Byte value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromByteArray(byte[] value)
          Creates an instance based on the provided byte array.
static Bytes fromByteBuffer(ByteBuffer value)
          Creates an instance based on the provided byte buffer.
static Bytes fromBytes(byte[] value)
          Creates an instance based on the provided byte array.
static Bytes fromChar(char value)
          Creates an instance based on the provided value.
static Bytes fromChar(Character value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromDouble(double value)
          Creates an instance based on the provided value.
static Bytes fromDouble(Double value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromFloat(float value)
          Creates an instance based on the provided value.
static Bytes fromFloat(Float value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromInt(int value)
          Creates an instance based on the provided value.
static Bytes fromInt(Integer value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromLong(long value)
          Creates an instance based on the provided value.
static Bytes fromLong(Long value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromShort(short value)
          Creates an instance based on the provided value.
static Bytes fromShort(Short value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromTimeUuid(long time, long clockSeqAndNode)
          Creates an instance based on the provided values.
static Bytes fromTimeUuid(String value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromTimeUuid(com.eaio.uuid.UUID value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromUTF8(String value)
          Creates an instance based on the provided value in UTF-8 format handling nulls.
static Bytes fromUuid(long msb, long lsb)
          Creates an instance based on the provided values.
static Bytes fromUuid(String value)
          Creates an instance based on the provided value handling nulls.
static Bytes fromUuid(UUID value)
          Creates an instance based on the provided value handling nulls.
 ByteBuffer getBytes()
          Returns the underlying ByteBuffer.
 int hashCode()
          Calculates the hash code using Arrays.hashCode(byte[]).
 boolean isNull()
          Helper used to determine if the underlying ByteBuffer is null.
 int length()
          Returns the length of the value within the underlying byte buffer.
static ByteBuffer nullSafeGet(Bytes bytes)
          Returns the underlying byte array of the provided bytes instance or null if the provided instance was null.
 boolean toBoolean()
          Converts the backing array to the appropriate primitive.
 Boolean toBoolean(Boolean defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 byte toByte()
          Converts the backing array to the appropriate primitive.
 Byte toByte(Byte defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 byte[] toByteArray()
          Converts the backing array to the appropriate primitive.
 byte[] toByteArray(byte[] defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 char toChar()
          Converts the backing array to the appropriate primitive.
 Character toChar(Character defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 double toDouble()
          Converts the backing array to the appropriate primitive.
 Double toDouble(Double defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 float toFloat()
          Converts the backing array to the appropriate primitive.
 Float toFloat(Float defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 int toInt()
          Converts the backing array to the appropriate primitive.
 Integer toInt(Integer defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 long toLong()
          Converts the backing array to the appropriate primitive.
 Long toLong(Long defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 short toShort()
          Converts the backing array to the appropriate primitive.
 Short toShort(Short defaultIfNull)
          Converts the backing array to the appropriate object instance handling nulls.
 String toString()
          Returns an (expensive) string representation of the bytes as defined by the Arrays.toString(byte[]) method.
 com.eaio.uuid.UUID toTimeUuid()
          Converts the backing array to the appropriate object instance handling nulls.
 String toUTF8()
          Converts the backing array to the appropriate object instance handling nulls.
static String toUTF8(byte[] bytes)
          Convert a raw byte array to a UTF-8 string
static String toUTF8(ByteBuffer bytes)
          Convert the byte buffer to a UTF-8 string.
 UUID toUuid()
          Converts the backing array to the appropriate object instance handling nulls.
static List<ByteBuffer> transformBytesToList(Collection<Bytes> arrays)
          Transforms the provided list of Bytes instances into a list of byte arrays.
static Set<ByteBuffer> transformBytesToSet(Collection<Bytes> arrays)
          Transforms the provided list of Bytes instances into a list of byte arrays.
static List<ByteBuffer> transformUTF8ToList(Collection<String> strings)
          Transforms the provided list of String instances into a list of byte arrays.
static Set<ByteBuffer> transformUTF8ToSet(Collection<String> strings)
          Transforms the provided list of String instances into a list of byte arrays.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY

public static final Bytes EMPTY

NULL

public static final Bytes NULL
Constructor Detail

Bytes

public Bytes(byte[] bytes)
Constructs a new instance based on the provided byte array.

Parameters:
bytes - the bytes

Bytes

public Bytes(ByteBuffer bytes)
Constructs a new instance based on the provided byte buffer. The Buffer.position() must be in the correct position to read the appropriate value from it.

Parameters:
bytes - the bytes
Method Detail

toString

public String toString()
Returns an (expensive) string representation of the bytes as defined by the Arrays.toString(byte[]) method.

NOTE: The toUTF8() method provides the reverse value of the fromUTF8(String) method.

Overrides:
toString in class Object
Returns:
the string representation

getBytes

public ByteBuffer getBytes()
Returns the underlying ByteBuffer.

Returns:
the raw byte array

duplicate

public Bytes duplicate()
Returns a duplicate of the bytes instance. The underlying ByteBuffer is duplicated using the ByteBuffer.duplicate() method.

Returns:
the raw byte array
See Also:
ByteBuffer.duplicate()

equals

public boolean equals(Object o)
Determines if two instances of this class are equals using the Arrays.equals(byte[], byte[]) method.

Overrides:
equals in class Object
Parameters:
o - the other instance
Returns:
true if they are equal, otherwise false

hashCode

public int hashCode()
Calculates the hash code using Arrays.hashCode(byte[]).

Note that the instances hashCode is calculated the first time this method is called and then cached.

Overrides:
hashCode in class Object
Returns:
the hash code

length

public int length()
Returns the length of the value within the underlying byte buffer.

Returns:
the length

fromBytes

public static Bytes fromBytes(byte[] value)
Creates an instance based on the provided byte array.

Parameters:
value - the value
Returns:
the Bytes instance

fromByteArray

public static Bytes fromByteArray(byte[] value)
Creates an instance based on the provided byte array.

Parameters:
value - the value
Returns:
the Bytes instance

fromByteBuffer

public static Bytes fromByteBuffer(ByteBuffer value)
Creates an instance based on the provided byte buffer.

Parameters:
value - the value
Returns:
the Bytes instance

fromChar

public static Bytes fromChar(char value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromChar

public static Bytes fromChar(Character value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromByte

public static Bytes fromByte(byte value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromByte

public static Bytes fromByte(Byte value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromLong

public static Bytes fromLong(long value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromLong

public static Bytes fromLong(Long value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromInt

public static Bytes fromInt(int value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromInt

public static Bytes fromInt(Integer value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromShort

public static Bytes fromShort(short value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromShort

public static Bytes fromShort(Short value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromDouble

public static Bytes fromDouble(double value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromDouble

public static Bytes fromDouble(Double value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromFloat

public static Bytes fromFloat(float value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromFloat

public static Bytes fromFloat(Float value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromBoolean

public static Bytes fromBoolean(boolean value)
Creates an instance based on the provided value.

Parameters:
value - the value
Returns:
the instance
See Also:
for details on serializaion format

fromBoolean

public static Bytes fromBoolean(Boolean value)
Creates an instance based on the provided value handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on serializaion format

fromUuid

public static Bytes fromUuid(UUID value)
Creates an instance based on the provided value handling nulls. If the provided value is not null then this method delegates to fromUuid(long, long) to perform the deserialization.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on long serializaion format

fromUuid

public static Bytes fromUuid(String value)
Creates an instance based on the provided value handling nulls. If the provided value is not null then a new instance of UUID is created using UUID.fromString(String) and then fromUuid(long, long) is called to perform the deserialization.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on long serializaion format

fromUuid

public static Bytes fromUuid(long msb,
                             long lsb)
Creates an instance based on the provided values. The ByteBuffer.putLong(long) is called twice, the first call for the msb value and second for the lsb value.

Parameters:
msb - the msb value
lsb - the lsb value
Returns:
the instance or null if the value provided was null
See Also:
for details on long serializaion format

fromTimeUuid

public static Bytes fromTimeUuid(com.eaio.uuid.UUID value)
Creates an instance based on the provided value handling nulls. If the provided value is not null then this method delegates to fromTimeUuid(long, long) to perform the deserialization.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on long serializaion format

fromTimeUuid

public static Bytes fromTimeUuid(String value)
Creates an instance based on the provided value handling nulls. If the provided value is not null then a new instance of UUID is created and then fromTimeUuid(long, long) is called to perform the deserialization.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on long serializaion format

fromTimeUuid

public static Bytes fromTimeUuid(long time,
                                 long clockSeqAndNode)
Creates an instance based on the provided values. The ByteBuffer.putLong(long) is called twice, the first call for the time value and second for the clockSeqAndNode value.

Parameters:
time - the time value
clockSeqAndNode - the clockSeqAndNode value
Returns:
the instance or null if the value provided was null
See Also:
for details on long serializaion format

fromUTF8

public static Bytes fromUTF8(String value)
Creates an instance based on the provided value in UTF-8 format handling nulls.

Parameters:
value - the value
Returns:
the instance or null if the value provided was null
See Also:
for details on the format

toChar

public Character toChar(Character defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toChar

public char toChar()
            throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toByteArray

public byte[] toByteArray(byte[] defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toByteArray

public byte[] toByteArray()
                   throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toByte

public Byte toByte(Byte defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toByte

public byte toByte()
            throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toLong

public Long toLong(Long defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toLong

public long toLong()
            throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toInt

public Integer toInt(Integer defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toInt

public int toInt()
          throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toShort

public Short toShort(Short defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toShort

public short toShort()
              throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toDouble

public Double toDouble(Double defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toDouble

public double toDouble()
                throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toFloat

public Float toFloat(Float defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toFloat

public float toFloat()
              throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toBoolean

public Boolean toBoolean(Boolean defaultIfNull)
Converts the backing array to the appropriate object instance handling nulls.

Parameters:
defaultIfNull - the value to return is the backing array is null
Returns:
the deserialized instance

toBoolean

public boolean toBoolean()
                  throws IllegalStateException
Converts the backing array to the appropriate primitive.

Returns:
the deserialized primitive
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toUuid

public UUID toUuid()
            throws IllegalStateException
Converts the backing array to the appropriate object instance handling nulls.

Returns:
the deserialized instance or null if the backing array was null
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toTimeUuid

public com.eaio.uuid.UUID toTimeUuid()
                              throws IllegalStateException
Converts the backing array to the appropriate object instance handling nulls.

Returns:
the deserialized instance or null if the backing array was null
Throws:
IllegalStateException - if the underlying array does not contain the appropriate data

toUTF8

public String toUTF8()
Converts the backing array to the appropriate object instance handling nulls.

Note: we could potentially depend on the org.apache.cassandra.utils.ByteBufferUtil#string(java.nio.ByteBuffer, java.nio.charset.Charset). Is it a public API?

Returns:
the deserialized instance or null if the backing array was null

toUTF8

public static String toUTF8(ByteBuffer bytes)
Convert the byte buffer to a UTF-8 string. The byte buffer will maintain it's original Buffer.position().

Parameters:
bytes - The UTF-8 string, encoded as raw bytes
Returns:
The UTF-8 string object represented by the byte array

toUTF8

public static String toUTF8(byte[] bytes)
Convert a raw byte array to a UTF-8 string

Parameters:
bytes - The UTF-8 string, encoded as raw bytes
Returns:
The UTF-8 string object represented by the byte array

transformBytesToSet

public static Set<ByteBuffer> transformBytesToSet(Collection<Bytes> arrays)
Transforms the provided list of Bytes instances into a list of byte arrays.

Parameters:
arrays - the list of Bytes instances
Returns:
the list of byte arrays

transformBytesToList

public static List<ByteBuffer> transformBytesToList(Collection<Bytes> arrays)
Transforms the provided list of Bytes instances into a list of byte arrays.

Parameters:
arrays - the list of Bytes instances
Returns:
the list of byte arrays

transformUTF8ToSet

public static Set<ByteBuffer> transformUTF8ToSet(Collection<String> strings)
Transforms the provided list of String instances into a list of byte arrays.

Parameters:
strings - the list of String instances
Returns:
the list of byte arrays

transformUTF8ToList

public static List<ByteBuffer> transformUTF8ToList(Collection<String> strings)
Transforms the provided list of String instances into a list of byte arrays.

Parameters:
strings - the list of String instances
Returns:
the list of byte arrays

nullSafeGet

public static ByteBuffer nullSafeGet(Bytes bytes)
Returns the underlying byte array of the provided bytes instance or null if the provided instance was null.

Parameters:
bytes - the bytes instance
Returns:
the underlying byte array of the instance or null

isNull

public boolean isNull()
Helper used to determine if the underlying ByteBuffer is null.

Returns:
true if null, otherwise false


Copyright © 2011. All Rights Reserved.