org.hbase.async
Class Bytes

java.lang.Object
  extended by org.hbase.async.Bytes

public final class Bytes
extends Object

Helper functions to manipulate byte arrays.


Nested Class Summary
static class Bytes.ByteMap<V>
          A convenient map keyed with a byte array.
 
Field Summary
static org.hbase.async.Bytes.MemCmp MEMCMP
          A singleton Comparator for non-null byte arrays.
 
Method Summary
static byte[] deDup(byte[] old, byte[] neww)
          De-duplicates two byte arrays.
static boolean equals(byte[] a, byte[] b)
          Tests whether two byte arrays have the same contents.
static byte[] fromInt(int n)
          Creates a new byte array containing a big-endian 4-byte integer.
static byte[] fromLong(long n)
          Creates a new byte array containing a big-endian 8-byte long integer.
static byte[] fromShort(short n)
          Creates a new byte array containing a big-endian 2-byte short integer.
static int getInt(byte[] b)
          Reads a big-endian 4-byte integer from the begining of the given array.
static int getInt(byte[] b, int offset)
          Reads a big-endian 4-byte integer from an offset in the given array.
static long getLong(byte[] b)
          Reads a big-endian 8-byte long from the begining of the given array.
static long getLong(byte[] b, int offset)
          Reads a big-endian 8-byte long from an offset in the given array.
static short getShort(byte[] b)
          Reads a big-endian 2-byte short from the begining of the given array.
static short getShort(byte[] b, int offset)
          Reads a big-endian 2-byte short from an offset in the given array.
static long getUnsignedInt(byte[] b)
          Reads a big-endian 4-byte unsigned integer from the begining of the given array.
static long getUnsignedInt(byte[] b, int offset)
          Reads a big-endian 4-byte unsigned integer from an offset in the given array.
static int getUnsignedShort(byte[] b)
          Reads a big-endian 2-byte unsigned short from the begining of the given array.
static int getUnsignedShort(byte[] b, int offset)
          Reads a big-endian 2-byte unsigned short from an offset in the given array.
static String hex(long v)
          Pretty-prints a long into a fixed-width hexadecimal number.
static byte[] ISO88591(String s)
          Transforms a string into an ISO-8859-1 encoded byte array.
static int memcmp(byte[] a, byte[] b)
          memcmp in Java, hooray.
static int memcmp(byte[] a, byte[] b, int offset, int length)
          memcmp(3) with a given offset and length.
static int memcmpMaybeNull(byte[] a, byte[] b)
          memcmp(3) in Java for possibly null arrays, hooray.
static String pretty(byte[] array)
          Pretty-prints a byte array into a human-readable string.
static String pretty(ChannelBuffer buf)
          Pretty-prints all the bytes of a buffer into a human-readable string.
static void pretty(StringBuilder outbuf, byte[] array)
          Pretty-prints a byte array into a human-readable output buffer.
static void pretty(StringBuilder outbuf, byte[][] arrays)
          Pretty-prints an array of byte arrays into a human-readable output buffer.
static void setInt(byte[] b, int n)
          Writes a big-endian 4-byte int at the begining of the given array.
static void setInt(byte[] b, int n, int offset)
          Writes a big-endian 4-byte int at an offset in the given array.
static void setLong(byte[] b, long n)
          Writes a big-endian 8-byte long at the begining of the given array.
static void setLong(byte[] b, long n, int offset)
          Writes a big-endian 8-byte long at an offset in the given array.
static void setShort(byte[] b, short n)
          Writes a big-endian 2-byte short at the begining of the given array.
static void setShort(byte[] b, short n, int offset)
          Writes a big-endian 2-byte short at an offset in the given array.
static byte[] UTF8(String s)
          Transforms a string into an UTF-8 encoded byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MEMCMP

public static final org.hbase.async.Bytes.MemCmp MEMCMP
A singleton Comparator for non-null byte arrays.

See Also:
memcmp(byte[], byte[])
Method Detail

getShort

public static short getShort(byte[] b)
Reads a big-endian 2-byte short from the begining of the given array.

Parameters:
b - The array to read from.
Returns:
A short integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

getShort

public static short getShort(byte[] b,
                             int offset)
Reads a big-endian 2-byte short from an offset in the given array.

Parameters:
b - The array to read from.
offset - The offset in the array to start reading from.
Returns:
A short integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

getUnsignedShort

public static int getUnsignedShort(byte[] b)
Reads a big-endian 2-byte unsigned short from the begining of the given array.

Parameters:
b - The array to read from.
Returns:
A positive short integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

getUnsignedShort

public static int getUnsignedShort(byte[] b,
                                   int offset)
Reads a big-endian 2-byte unsigned short from an offset in the given array.

Parameters:
b - The array to read from.
offset - The offset in the array to start reading from.
Returns:
A positive short integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

setShort

public static void setShort(byte[] b,
                            short n)
Writes a big-endian 2-byte short at the begining of the given array.

Parameters:
b - The array to write to.
n - A short integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

setShort

public static void setShort(byte[] b,
                            short n,
                            int offset)
Writes a big-endian 2-byte short at an offset in the given array.

Parameters:
b - The array to write to.
offset - The offset in the array to start writing at.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

fromShort

public static byte[] fromShort(short n)
Creates a new byte array containing a big-endian 2-byte short integer.

Parameters:
n - A short integer.
Returns:
A new byte array containing the given value.

getInt

public static int getInt(byte[] b)
Reads a big-endian 4-byte integer from the begining of the given array.

Parameters:
b - The array to read from.
Returns:
An integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

getInt

public static int getInt(byte[] b,
                         int offset)
Reads a big-endian 4-byte integer from an offset in the given array.

Parameters:
b - The array to read from.
offset - The offset in the array to start reading from.
Returns:
An integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

getUnsignedInt

public static long getUnsignedInt(byte[] b)
Reads a big-endian 4-byte unsigned integer from the begining of the given array.

Parameters:
b - The array to read from.
Returns:
A positive integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

getUnsignedInt

public static long getUnsignedInt(byte[] b,
                                  int offset)
Reads a big-endian 4-byte unsigned integer from an offset in the given array.

Parameters:
b - The array to read from.
offset - The offset in the array to start reading from.
Returns:
A positive integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

setInt

public static void setInt(byte[] b,
                          int n)
Writes a big-endian 4-byte int at the begining of the given array.

Parameters:
b - The array to write to.
n - An integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

setInt

public static void setInt(byte[] b,
                          int n,
                          int offset)
Writes a big-endian 4-byte int at an offset in the given array.

Parameters:
b - The array to write to.
offset - The offset in the array to start writing at.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

fromInt

public static byte[] fromInt(int n)
Creates a new byte array containing a big-endian 4-byte integer.

Parameters:
n - An integer.
Returns:
A new byte array containing the given value.

getLong

public static long getLong(byte[] b)
Reads a big-endian 8-byte long from the begining of the given array.

Parameters:
b - The array to read from.
Returns:
A long integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

getLong

public static long getLong(byte[] b,
                           int offset)
Reads a big-endian 8-byte long from an offset in the given array.

Parameters:
b - The array to read from.
offset - The offset in the array to start reading from.
Returns:
A long integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

setLong

public static void setLong(byte[] b,
                           long n)
Writes a big-endian 8-byte long at the begining of the given array.

Parameters:
b - The array to write to.
n - A long integer.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

setLong

public static void setLong(byte[] b,
                           long n,
                           int offset)
Writes a big-endian 8-byte long at an offset in the given array.

Parameters:
b - The array to write to.
offset - The offset in the array to start writing at.
Throws:
IndexOutOfBoundsException - if the byte array is too small.

fromLong

public static byte[] fromLong(long n)
Creates a new byte array containing a big-endian 8-byte long integer.

Parameters:
n - A long integer.
Returns:
A new byte array containing the given value.

UTF8

public static byte[] UTF8(String s)
Transforms a string into an UTF-8 encoded byte array.


ISO88591

public static byte[] ISO88591(String s)
Transforms a string into an ISO-8859-1 encoded byte array.


pretty

public static void pretty(StringBuilder outbuf,
                          byte[] array)
Pretty-prints a byte array into a human-readable output buffer.

Parameters:
outbuf - The buffer where to write the output.
array - The (possibly null) array to pretty-print.

pretty

public static void pretty(StringBuilder outbuf,
                          byte[][] arrays)
Pretty-prints an array of byte arrays into a human-readable output buffer.

Parameters:
outbuf - The buffer where to write the output.
arrays - The (possibly null) array of arrays to pretty-print.
Since:
1.3

pretty

public static String pretty(byte[] array)
Pretty-prints a byte array into a human-readable string.

Parameters:
array - The (possibly null) array to pretty-print.
Returns:
The array in a pretty-printed string.

hex

public static String hex(long v)
Pretty-prints a long into a fixed-width hexadecimal number.

Returns:
A string of the form 0x0123456789ABCDEF.

pretty

public static String pretty(ChannelBuffer buf)
Pretty-prints all the bytes of a buffer into a human-readable string.

Parameters:
buf - The (possibly null) buffer to pretty-print.
Returns:
The buffer in a pretty-printed string.

memcmp

public static int memcmp(byte[] a,
                         byte[] b)
memcmp in Java, hooray.

Parameters:
a - First non-null byte array to compare.
b - Second non-null byte array to compare.
Returns:
0 if the two arrays are identical, otherwise the difference between the first two different bytes, otherwise the different between their lengths.

memcmp

public static int memcmp(byte[] a,
                         byte[] b,
                         int offset,
                         int length)
memcmp(3) with a given offset and length.

Parameters:
a - First non-null byte array to compare.
b - Second non-null byte array to compare.
offset - The offset at which to start comparing both arrays.
length - The number of bytes to compare.
Returns:
0 if the two arrays are identical, otherwise the difference between the first two different bytes (treated as unsigned), otherwise the different between their lengths.
Throws:
IndexOutOfBoundsException - if either array isn't large enough.

deDup

public static byte[] deDup(byte[] old,
                           byte[] neww)
De-duplicates two byte arrays.

If two byte arrays have the same contents but are different, this function helps to re-use the old one and discard the new copy.

Parameters:
old - The existing byte array.
neww - The new byte array we're trying to de-duplicate.
Returns:
old if neww is a different array with the same contents, otherwise neww.

equals

public static boolean equals(byte[] a,
                             byte[] b)
Tests whether two byte arrays have the same contents.

Parameters:
a - First non-null byte array to compare.
b - Second non-null byte array to compare.
Returns:
true if the two arrays are identical, false otherwise.

memcmpMaybeNull

public static int memcmpMaybeNull(byte[] a,
                                  byte[] b)
memcmp(3) in Java for possibly null arrays, hooray.

Parameters:
a - First possibly null byte array to compare.
b - Second possibly null byte array to compare.
Returns:
0 if the two arrays are identical (or both are null), otherwise the difference between the first two different bytes (treated as unsigned), otherwise the different between their lengths (a null byte array is considered shorter than an empty byte array).