org.h2.util
Class ByteUtils

java.lang.Object
  extended by org.h2.util.ByteUtils

public class ByteUtils
extends java.lang.Object

This utility class contains functions related to byte arrays.


Method Summary
static void clear(byte[] buff)
          Set all elements of the array to zero.
static byte[] cloneByteArray(byte[] b)
          Create a new byte array and copy all the data.
static int compareNotNull(byte[] data1, byte[] data2)
          Compare the contents of two byte arrays.
static boolean compareSecure(byte[] test, byte[] good)
          Compare two byte arrays.
static java.lang.String convertBytesToString(byte[] value)
          Convert a byte array to a hex encoded string.
static java.lang.String convertBytesToString(byte[] value, int len)
          Convert a byte array to a hex encoded string.
static byte[] convertStringToBytes(java.lang.String s)
          Convert a hex encoded string to a byte array.
static byte[] copy(byte[] source, byte[] target)
          Copy the contents of the source array to the target array.
static int getByteArrayHash(byte[] value)
          Calculate the hash code of the given byte array.
static int indexOf(byte[] bytes, byte[] pattern, int start)
          Calculate the index of the first occurrence of the pattern in the byte array, starting with the given index.
static long readLong(byte[] buff, int pos)
          Read a long value from the byte array at the given position.
static void writeLong(byte[] buff, int pos, long x)
          Write a long value to the byte array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readLong

public static long readLong(byte[] buff,
                            int pos)
Read a long value from the byte array at the given position. The most significant byte is read first.

Parameters:
buff - the byte array
pos - the position
Returns:
the value

writeLong

public static void writeLong(byte[] buff,
                             int pos,
                             long x)
Write a long value to the byte array.

Parameters:
buff - the byte array
pos - the position
x - the value

indexOf

public static int indexOf(byte[] bytes,
                          byte[] pattern,
                          int start)
Calculate the index of the first occurrence of the pattern in the byte array, starting with the given index. This methods returns -1 if the pattern has not been found, and the start position if the pattern is empty.

Parameters:
bytes - the byte array
pattern - the pattern
start - the start index from where to search
Returns:
the index

convertStringToBytes

public static byte[] convertStringToBytes(java.lang.String s)
                                   throws java.sql.SQLException
Convert a hex encoded string to a byte array.

Parameters:
s - the hex encoded string
Returns:
the byte array
Throws:
java.sql.SQLException

getByteArrayHash

public static int getByteArrayHash(byte[] value)
Calculate the hash code of the given byte array.

Parameters:
value - the byte array
Returns:
the hash code

convertBytesToString

public static java.lang.String convertBytesToString(byte[] value)
Convert a byte array to a hex encoded string.

Parameters:
value - the byte array
Returns:
the hex encoded string

convertBytesToString

public static java.lang.String convertBytesToString(byte[] value,
                                                    int len)
Convert a byte array to a hex encoded string.

Parameters:
value - the byte array
len - the number of bytes to encode
Returns:
the hex encoded string

compareSecure

public static boolean compareSecure(byte[] test,
                                    byte[] good)
Compare two byte arrays. This method will always loop over all bytes and doesn't use conditional operations in the loop to make sure an attacker can not use a timing attack when trying out passwords.

Parameters:
test - the first array
good - the second array
Returns:
true if both byte arrays contain the same bytes

clear

public static void clear(byte[] buff)
Set all elements of the array to zero.

Parameters:
buff - the byte array

compareNotNull

public static int compareNotNull(byte[] data1,
                                 byte[] data2)
Compare the contents of two byte arrays. If the content or length of the first array is smaller than the second array, -1 is returned. If the content or length of the second array is smaller than the first array, 1 is returned. If the contents and lengths are the same, 0 is returned.

Parameters:
data1 - the first byte array (must not be null)
data2 - the second byte array (must not be null)
Returns:
the result of the comparison (-1, 1 or 0)

copy

public static byte[] copy(byte[] source,
                          byte[] target)
Copy the contents of the source array to the target array. If the size if the target array is too small, a larger array is created.

Parameters:
source - the source array
target - the target array
Returns:
the target array or a new one if the target array was too small

cloneByteArray

public static byte[] cloneByteArray(byte[] b)
Create a new byte array and copy all the data. If the size of the byte array is zero, the same array is returned.

Parameters:
b - the byte array (may not be null)
Returns:
a new byte array