|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.scale7.cassandra.pelops.Bytes
public class Bytes
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).
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 |
---|
public static final Bytes EMPTY
public static final Bytes NULL
Constructor Detail |
---|
public Bytes(byte[] bytes)
bytes
- the bytespublic Bytes(ByteBuffer bytes)
Buffer.position()
must be
in the correct position to read the appropriate value from it.
bytes
- the bytesMethod Detail |
---|
public String toString()
Arrays.toString(byte[])
method.
NOTE: The toUTF8()
method provides the reverse value of the fromUTF8(String)
method.
toString
in class Object
public ByteBuffer getBytes()
ByteBuffer
.
public Bytes duplicate()
ByteBuffer
is duplicated using the
ByteBuffer.duplicate()
method.
ByteBuffer.duplicate()
public boolean equals(Object o)
Arrays.equals(byte[], byte[])
method.
equals
in class Object
o
- the other instance
public int hashCode()
Arrays.hashCode(byte[])
.
Note that the instances hashCode is calculated the first time this method is called and then cached.
hashCode
in class Object
public int length()
public static Bytes fromBytes(byte[] value)
value
- the value
public static Bytes fromByteArray(byte[] value)
value
- the value
public static Bytes fromByteBuffer(ByteBuffer value)
value
- the value
public static Bytes fromChar(char value)
value
- the value
for details on serializaion format
public static Bytes fromChar(Character value)
value
- the value
for details on serializaion format
public static Bytes fromByte(byte value)
value
- the value
for details on serializaion format
public static Bytes fromByte(Byte value)
value
- the value
for details on serializaion format
public static Bytes fromLong(long value)
value
- the value
for details on serializaion format
public static Bytes fromLong(Long value)
value
- the value
for details on serializaion format
public static Bytes fromInt(int value)
value
- the value
for details on serializaion format
public static Bytes fromInt(Integer value)
value
- the value
for details on serializaion format
public static Bytes fromShort(short value)
value
- the value
for details on serializaion format
public static Bytes fromShort(Short value)
value
- the value
for details on serializaion format
public static Bytes fromDouble(double value)
value
- the value
for details on serializaion format
public static Bytes fromDouble(Double value)
value
- the value
for details on serializaion format
public static Bytes fromFloat(float value)
value
- the value
for details on serializaion format
public static Bytes fromFloat(Float value)
value
- the value
for details on serializaion format
public static Bytes fromBoolean(boolean value)
value
- the value
for details on serializaion format
public static Bytes fromBoolean(Boolean value)
value
- the value
for details on serializaion format
public static Bytes fromUuid(UUID value)
fromUuid(long, long)
to perform the deserialization.
value
- the value
for details on long serializaion format
public static Bytes fromUuid(String value)
UUID
is created using UUID.fromString(String)
and then
fromUuid(long, long)
is called to perform the deserialization.
value
- the value
for details on long serializaion format
public static Bytes fromUuid(long msb, long lsb)
ByteBuffer.putLong(long)
is called twice,
the first call for the msb value and second for the lsb value.
msb
- the msb valuelsb
- the lsb value
for details on long serializaion format
public static Bytes fromTimeUuid(com.eaio.uuid.UUID value)
fromTimeUuid(long, long)
to perform the deserialization.
value
- the value
for details on long serializaion format
public static Bytes fromTimeUuid(String value)
UUID
is created and then
fromTimeUuid(long, long)
is called to perform the deserialization.
value
- the value
for details on long serializaion format
public static Bytes fromTimeUuid(long time, long clockSeqAndNode)
ByteBuffer.putLong(long)
is called twice,
the first call for the time value and second for the clockSeqAndNode value.
time
- the time valueclockSeqAndNode
- the clockSeqAndNode value
for details on long serializaion format
public static Bytes fromUTF8(String value)
value
- the value
for details on the format
public Character toChar(Character defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public char toChar() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic byte[] toByteArray(byte[] defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public byte[] toByteArray() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic Byte toByte(Byte defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public byte toByte() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic Long toLong(Long defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public long toLong() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic Integer toInt(Integer defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public int toInt() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic Short toShort(Short defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public short toShort() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic Double toDouble(Double defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public double toDouble() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic Float toFloat(Float defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public float toFloat() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic Boolean toBoolean(Boolean defaultIfNull)
defaultIfNull
- the value to return is the backing array is null
public boolean toBoolean() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic UUID toUuid() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic com.eaio.uuid.UUID toTimeUuid() throws IllegalStateException
IllegalStateException
- if the underlying array does not contain the appropriate datapublic String toUTF8()
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?
public static String toUTF8(ByteBuffer bytes)
Buffer.position()
.
bytes
- The UTF-8 string, encoded as raw bytes
public static String toUTF8(byte[] bytes)
bytes
- The UTF-8 string, encoded as raw bytes
public static Set<ByteBuffer> transformBytesToSet(Collection<Bytes> arrays)
Bytes
instances into a list of byte arrays.
arrays
- the list of Bytes instances
public static List<ByteBuffer> transformBytesToList(Collection<Bytes> arrays)
Bytes
instances into a list of byte arrays.
arrays
- the list of Bytes instances
public static Set<ByteBuffer> transformUTF8ToSet(Collection<String> strings)
String
instances into a list of byte arrays.
strings
- the list of String instances
public static List<ByteBuffer> transformUTF8ToList(Collection<String> strings)
String
instances into a list of byte arrays.
strings
- the list of String instances
public static ByteBuffer nullSafeGet(Bytes bytes)
bytes
- the bytes instance
public boolean isNull()
ByteBuffer
is null.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |