voldemort.utils
Class ByteUtils

java.lang.Object
  extended by voldemort.utils.ByteUtils

public class ByteUtils
extends java.lang.Object

Utility functions for munging on bytes


Field Summary
static long BYTES_PER_GB
           
static int BYTES_PER_MB
           
static int MASK_00000000
           
static int MASK_00011111
           
static int MASK_00111111
           
static int MASK_01000000
           
static int MASK_01011111
           
static int MASK_01100000
           
static int MASK_01101111
           
static int MASK_01110000
           
static int MASK_01111111
           
static int MASK_10000000
           
static int MASK_10111111
           
static int MASK_11000000
           
static int MASK_11011111
           
static int MASK_11100000
           
static int MASK_11111111
           
static int SIZE_OF_BYTE
           
static int SIZE_OF_INT
           
static int SIZE_OF_LONG
           
static int SIZE_OF_SHORT
           
 
Constructor Summary
ByteUtils()
           
 
Method Summary
static byte[] cat(byte[]... arrays)
          Concatenate the given arrays TODO: cut and paste for every primative type and Object (yay! java!)
static int compare(byte[] b1, byte[] b2)
          Compare two byte arrays.
static int compare(byte[] b1, byte[] b2, int offset, int to)
          Compare a byte array ( b1 ) with a sub
static byte[] copy(byte[] array, int from, int to)
          Copy the specified bytes into a new array
static java.nio.ByteBuffer expand(java.nio.ByteBuffer buffer, int newCapacity)
          If we have no more room in the current buffer, then double our capacity and copy the current buffer to the new one.
static byte[] fromHexString(java.lang.String hexString)
           
static byte[] getBytes(java.lang.String string, java.lang.String encoding)
          Translate the string to bytes using the given encoding
static java.security.MessageDigest getDigest(java.lang.String algorithm)
           
static java.lang.String getString(byte[] bytes, java.lang.String encoding)
          Create a string from bytes using the given encoding
static byte[] md5(byte[] input)
          Compute the md5 hash of the input catching all the irritating exceptions for you
static byte numberOfBytesRequired(long number)
          The number of bytes required to hold the given number
static void read(java.io.InputStream stream, byte[] buffer)
          Read exactly buffer.length bytes from the stream into the buffer
static long readBytes(byte[] bytes, int offset, int numBytes)
          Read the given number of bytes into a long
static int readInt(byte[] bytes, int offset)
          Read an int from the byte array starting at the given offset
static long readLong(byte[] bytes, int offset)
          Read a long from the byte array starting at the given offset
static byte readNthByte(long l, int n)
          Get the nth byte from the right in the given number
static short readShort(byte[] bytes, int offset)
          Read a short from the byte array starting at the given offset
static long readUnsignedInt(byte[] bytes, int offset)
          Read an unsigned integer from the given byte array
static int readUnsignedShort(byte[] bytes, int offset)
          Read an unsigned short from the byte array starting at the given offset
static long readVarNumber(java.io.DataInputStream input)
           
static byte[] sha1(byte[] input)
          Compute the sha1 hash of the input catching all the irritating exceptions for you
static java.lang.String toBinaryString(byte[] bytes)
          Translate the given byte array into a string of 1s and 0s
static java.lang.String toHexString(byte[] bytes)
          Translate the given byte array into a hexidecimal string
static void writeBytes(byte[] bytes, long value, int offset, int numBytes)
          Write the given number of bytes out to the array
static void writeInt(byte[] bytes, int value, int offset)
          Write an int to the byte array starting at the given offset
static void writeLong(byte[] bytes, long value, int offset)
          Write a long to the byte array starting at the given offset
static void writeShort(byte[] bytes, short value, int offset)
          Write a short to the byte array starting at the given offset
static void writeUnsignedShort(byte[] bytes, int value, int offset)
          Write an unsigned short to the byte array starting at the given offset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZE_OF_BYTE

public static final int SIZE_OF_BYTE
See Also:
Constant Field Values

SIZE_OF_SHORT

public static final int SIZE_OF_SHORT
See Also:
Constant Field Values

SIZE_OF_INT

public static final int SIZE_OF_INT
See Also:
Constant Field Values

SIZE_OF_LONG

public static final int SIZE_OF_LONG
See Also:
Constant Field Values

MASK_00000000

public static final int MASK_00000000

MASK_10000000

public static final int MASK_10000000

MASK_11000000

public static final int MASK_11000000

MASK_11100000

public static final int MASK_11100000

MASK_10111111

public static final int MASK_10111111

MASK_11011111

public static final int MASK_11011111

MASK_01000000

public static final int MASK_01000000

MASK_01100000

public static final int MASK_01100000

MASK_01110000

public static final int MASK_01110000

MASK_01011111

public static final int MASK_01011111

MASK_01101111

public static final int MASK_01101111

MASK_11111111

public static final int MASK_11111111

MASK_01111111

public static final int MASK_01111111

MASK_00111111

public static final int MASK_00111111

MASK_00011111

public static final int MASK_00011111

BYTES_PER_MB

public static final int BYTES_PER_MB
See Also:
Constant Field Values

BYTES_PER_GB

public static final long BYTES_PER_GB
See Also:
Constant Field Values
Constructor Detail

ByteUtils

public ByteUtils()
Method Detail

getDigest

public static java.security.MessageDigest getDigest(java.lang.String algorithm)

toHexString

public static java.lang.String toHexString(byte[] bytes)
Translate the given byte array into a hexidecimal string

Parameters:
bytes - The bytes to translate
Returns:
The string

fromHexString

public static byte[] fromHexString(java.lang.String hexString)
                            throws org.apache.commons.codec.DecoderException
Throws:
org.apache.commons.codec.DecoderException

toBinaryString

public static java.lang.String toBinaryString(byte[] bytes)
Translate the given byte array into a string of 1s and 0s

Parameters:
bytes - The bytes to translate
Returns:
The string

cat

public static byte[] cat(byte[]... arrays)
Concatenate the given arrays TODO: cut and paste for every primative type and Object (yay! java!)

Parameters:
arrays - The arrays to concatenate
Returns:
A concatenated array

copy

public static byte[] copy(byte[] array,
                          int from,
                          int to)
Copy the specified bytes into a new array

Parameters:
array - The array to copy from
from - The index in the array to begin copying from
to - The least index not copied
Returns:
A new byte[] containing the copied bytes

readShort

public static short readShort(byte[] bytes,
                              int offset)
Read a short from the byte array starting at the given offset

Parameters:
bytes - The byte array to read from
offset - The offset to start reading at
Returns:
The short read

readUnsignedShort

public static int readUnsignedShort(byte[] bytes,
                                    int offset)
Read an unsigned short from the byte array starting at the given offset

Parameters:
bytes - The byte array to read from
offset - The offset to start reading at
Returns:
The short read

readInt

public static int readInt(byte[] bytes,
                          int offset)
Read an int from the byte array starting at the given offset

Parameters:
bytes - The byte array to read from
offset - The offset to start reading at
Returns:
The int read

readUnsignedInt

public static long readUnsignedInt(byte[] bytes,
                                   int offset)
Read an unsigned integer from the given byte array

Parameters:
bytes - The bytes to read from
offset - The offset to begin reading at
Returns:
The integer as a long

readLong

public static long readLong(byte[] bytes,
                            int offset)
Read a long from the byte array starting at the given offset

Parameters:
bytes - The byte array to read from
offset - The offset to start reading at
Returns:
The long read

readBytes

public static long readBytes(byte[] bytes,
                             int offset,
                             int numBytes)
Read the given number of bytes into a long

Parameters:
bytes - The byte array to read from
offset - The offset at which to begin reading
numBytes - The number of bytes to read
Returns:
The long value read

writeShort

public static void writeShort(byte[] bytes,
                              short value,
                              int offset)
Write a short to the byte array starting at the given offset

Parameters:
bytes - The byte array
value - The short to write
offset - The offset to begin writing at

writeUnsignedShort

public static void writeUnsignedShort(byte[] bytes,
                                      int value,
                                      int offset)
Write an unsigned short to the byte array starting at the given offset

Parameters:
bytes - The byte array
value - The short to write
offset - The offset to begin writing at

writeInt

public static void writeInt(byte[] bytes,
                            int value,
                            int offset)
Write an int to the byte array starting at the given offset

Parameters:
bytes - The byte array
value - The int to write
offset - The offset to begin writing at

writeLong

public static void writeLong(byte[] bytes,
                             long value,
                             int offset)
Write a long to the byte array starting at the given offset

Parameters:
bytes - The byte array
value - The long to write
offset - The offset to begin writing at

writeBytes

public static void writeBytes(byte[] bytes,
                              long value,
                              int offset,
                              int numBytes)
Write the given number of bytes out to the array

Parameters:
bytes - The array to write to
value - The value to write from
offset - the offset into the array
numBytes - The number of bytes to write

numberOfBytesRequired

public static byte numberOfBytesRequired(long number)
The number of bytes required to hold the given number

Parameters:
number - The number being checked.
Returns:
The required number of bytes (must be 8 or less)

readVarNumber

public static long readVarNumber(java.io.DataInputStream input)
                          throws java.io.IOException
Throws:
java.io.IOException

readNthByte

public static byte readNthByte(long l,
                               int n)
Get the nth byte from the right in the given number

Parameters:
l - The number from which to extract the byte
n - The byte index
Returns:
The given byte

read

public static void read(java.io.InputStream stream,
                        byte[] buffer)
                 throws java.io.IOException
Read exactly buffer.length bytes from the stream into the buffer

Parameters:
stream - The stream to read from
buffer - The buffer to read into
Throws:
java.io.IOException

getBytes

public static byte[] getBytes(java.lang.String string,
                              java.lang.String encoding)
Translate the string to bytes using the given encoding

Parameters:
string - The string to translate
encoding - The encoding to use
Returns:
The bytes that make up the string

getString

public static java.lang.String getString(byte[] bytes,
                                         java.lang.String encoding)
Create a string from bytes using the given encoding

Parameters:
bytes - The bytes to create a string from
encoding - The encoding of the string
Returns:
The created string

md5

public static byte[] md5(byte[] input)
Compute the md5 hash of the input catching all the irritating exceptions for you

Parameters:
input - The input to take the hash of
Returns:
The MD5 hash of the input bytes

sha1

public static byte[] sha1(byte[] input)
Compute the sha1 hash of the input catching all the irritating exceptions for you

Parameters:
input - The input to take the hash of
Returns:
The sha1 hash of the input bytes

compare

public static int compare(byte[] b1,
                          byte[] b2)
Compare two byte arrays. Two arrays are equal if they are the same size and have the same contents. Otherwise b1 is smaller iff it is a prefix of b2 or for the first index i for which b1[i] != b2[i], b1[i] < b2[i].

bytes are considered unsigned. passing negative values into byte will cause them to be considered as unsigned two's complement value.

Parameters:
b1 - The first array
b2 - The second array
Returns:
-1 if b1 < b2, 1 if b1 > b2, and 0 if they are equal

compare

public static int compare(byte[] b1,
                          byte[] b2,
                          int offset,
                          int to)
Compare a byte array ( b1 ) with a sub

Parameters:
b1 - The first array
b2 - The second array
offset - The offset in b2 from which to compare
to - The least offset in b2 which we don't compare
Returns:
-1 if b1 < b2, 1 if b1 > b2, and 0 if they are equal

expand

public static java.nio.ByteBuffer expand(java.nio.ByteBuffer buffer,
                                         int newCapacity)
If we have no more room in the current buffer, then double our capacity and copy the current buffer to the new one.

Note: the new buffer is allocated using ByteBuffer.allocate.

Parameters:
buffer - The buffer from which to copy the contents
newCapacity - The new capacity size


Jay Kreps, Roshan Sumbaly, Alex Feinberg, Bhupesh Bansal, Lei Gao, Chinmay Soman, Vinoth Chandar, Zhongjie Wu