baobab.util
Class Bytes

java.lang.Object
  extended by baobab.util.Bytes

public final class Bytes
extends java.lang.Object

Static methods for managing byte arrays (all methods follow Big Endian order where most significant bits are in front).

Author:
Commons-Id Team

Field Summary
static int INT_TO_BYTES
           
static int LONG_TO_BYTES
           
 
Method Summary
static byte[] append(byte[] a, byte[] b)
          Appends two bytes array into one.
static boolean areEqual(byte[] a, byte[] b)
          Compares two byte arrays for equality.
static int compareTo(byte[] lhs, byte[] rhs)
          Compares two byte arrays as specified by Comparable.
static byte[] toBytes(int n)
          Returns a 4-byte array built from an int.
static byte[] toBytes(int n, byte[] b)
          Build a 4-byte array from an int.
static int toBytes(int n, byte[] b, int startPosition)
          Build a 4-byte array from an int.
static byte[] toBytes(long n)
          Returns a 8-byte array built from a long.
static byte[] toBytes(long n, byte[] b)
          Build a 8-byte array from a long.
static int toBytes(long n, byte[] b, int startPosition)
          Build a 8-byte array from a long.
static int toInt(byte[] b)
          Build an int from first 4 bytes of the array.
static int toInt(byte[] b, int start)
          Build an int from first 4 bytes of the array.
static long toLong(byte[] b)
          Build a long from first 8 bytes of the array.
static long toLong(byte[] b, int start)
          Build a long from first 8 bytes of the array.
static short toShort(byte[] b)
          Build a short from first 2 bytes of the array.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INT_TO_BYTES

public static final int INT_TO_BYTES
See Also:
Constant Field Values

LONG_TO_BYTES

public static final int LONG_TO_BYTES
See Also:
Constant Field Values
Method Detail

append

public static byte[] append(byte[] a,
                            byte[] b)
Appends two bytes array into one.

Parameters:
a - A byte[].
b - A byte[].
Returns:
A byte[].

toBytes

public static byte[] toBytes(long n)
Returns a 8-byte array built from a long.

Parameters:
n - The number to convert.
Returns:
A byte[].

toBytes

public static byte[] toBytes(long n,
                             byte[] b)
Build a 8-byte array from a long. No check is performed on the array length.

Parameters:
n - The number to convert.
b - The array to fill.
Returns:
A byte[].

toBytes

public static int toBytes(long n,
                          byte[] b,
                          int startPosition)
Build a 8-byte array from a long. No check is performed on the array length.

Parameters:
n - The number to convert.
b - The array to fill.
Returns:
A byte[].

toBytes

public static byte[] toBytes(int n)
Returns a 4-byte array built from an int.

Parameters:
n - The number to convert.
Returns:
A byte[].

toBytes

public static byte[] toBytes(int n,
                             byte[] b)
Build a 4-byte array from an int. No check is performed on the array length.

Parameters:
n - The number to convert.
b - The array to fill.
Returns:
A byte[].

toBytes

public static int toBytes(int n,
                          byte[] b,
                          int startPosition)
Build a 4-byte array from an int. No check is performed on the array length.

Parameters:
n - The number to convert.
b - The array to fill.
Returns:
A byte[].

toLong

public static long toLong(byte[] b)
Build a long from first 8 bytes of the array.

Parameters:
b - The byte[] to convert.
Returns:
A long.

toLong

public static long toLong(byte[] b,
                          int start)
Build a long from first 8 bytes of the array.

Parameters:
b - The byte[] to convert.
Returns:
A long.

toInt

public static int toInt(byte[] b)
Build an int from first 4 bytes of the array.

Parameters:
b - The byte[] to convert.
Returns:
An int.

toInt

public static int toInt(byte[] b,
                        int start)
Build an int from first 4 bytes of the array.

Parameters:
b - The byte[] to convert.
Returns:
An int.

areEqual

public static boolean areEqual(byte[] a,
                               byte[] b)
Compares two byte arrays for equality.

Parameters:
a - A byte[].
b - A byte[].
Returns:
True if the arrays have identical contents.

compareTo

public static int compareTo(byte[] lhs,
                            byte[] rhs)

Compares two byte arrays as specified by Comparable.

Parameters:
lhs - - left hand value in the comparison operation.
rhs - - right hand value in the comparison operation.
Returns:
a negative integer, zero, or a positive integer as lhs is less than, equal to, or greater than rhs.

toShort

public static short toShort(byte[] b)
Build a short from first 2 bytes of the array.

Parameters:
b - The byte[] to convert.
Returns:
A short.