ostore.util
Class ByteUtils

java.lang.Object
  |
  +--ostore.util.ByteUtils

public class ByteUtils
extends Object

ByteUtils is a class with commonly needed operations for converting various built-in types to bytes and back.

Version:
$Id: ByteUtils.java,v 1.38 2003/11/17 23:52:43 emilong Exp $
Author:
Sean C. Rhea, Dennis Geels

Field Summary
static char[] digits
          A nibble->char mapping for printing out bytes.
static int SIZE_BOOL
           
static int SIZE_DOUBLE
           
static int SIZE_INT
           
static int SIZE_LONG
           
static int SIZE_SHORT
           
 
Constructor Summary
ByteUtils()
           
 
Method Summary
static void bigIntegerToBytes(BigInteger n, byte[] data, int[] offset)
          Write the bytes representing n into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
static void boolToBytes(boolean b, byte[] data, int[] offset)
          Write the bytes representing b into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
static BigInteger bytesToBigInteger(byte[] data, int[] offset)
          Return the BigInteger represented by the bytes in data staring at offset offset[0].
static boolean bytesToBool(byte[] data, int[] offset)
          Return the boolean represented by the bytes in data staring at offset offset[0].
static double bytesToDouble(byte[] data, int[] offset)
          Return the double represented by the bytes in data staring at offset offset[0].
static int bytesToInt(byte[] data, int[] offset)
          Return the int represented by the bytes in data staring at offset offset[0].
static void bytesToInts(int[] dst, int dst_offset, byte[] src, int src_offset, int length)
          Convert an array of bytess into an array of ints.
static long bytesToLong(byte[] data, int[] offset)
          Return the long represented by the bytes in data staring at offset offset[0].
static short bytesToShort(byte[] data, int[] offset)
          Return the short represented by the bytes in data staring at offset offset[0].
static String bytesToString(byte[] data, int[] offset)
          Return the String represented by the bytes in data staring at offset offset[0].
static int byteToUnsignedInt(byte b)
          Convert a byte into an unsigned integer.
static void doubleToBytes(double d, byte[] data, int[] offset)
          Write the bytes representing d into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
static boolean equals(byte[] b1, byte[] b2)
          Determines whether two arrays of bytes contain the same contents.
static void intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length)
          Convert an array of ints into an array of bytes.
static void intToBytes(int i, byte[] data, int[] offset)
          Write the bytes representing i into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
static void longToBytes(long l, byte[] data, int[] offset)
          Write the bytes representing l into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
static void memclr(byte[] array, int offset, int length)
          Fill the given array with zeros.
static boolean memcmp(byte[] a, int a_offset, byte[] b, int b_offset, int length)
          Compare the contents of one array of bytes to another.
static int memcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length)
          Copy contents of one array of bytes into another.
static String print_bytes_exact(byte[] data, int offset, int length)
           
static String print_bytes(byte[] data)
           
static String print_bytes(byte[] data, int offset, int length)
          Produce a String representation for the specified array of bytes.
static QuickSerializable qs_clone(QuickSerializable qs)
          Clone a QuickSerializable object.
static int qs_length(QuickSerializable qs)
          Returns the length of the byte array required to store a QuickSerializable object.
static byte[] qs_to_bytes(QuickSerializable qs)
          Returns a byte array containing the serialized version of an object.
static int round_up(int value, int multiple)
          Round a number up to a given multiple.
static void shortToBytes(short s, byte[] data, int[] offset)
          Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
static void stringToBytes(String s, byte[] data, int[] offset)
          Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
static byte[] zero_pad(byte[] original, int block_size)
          Return a new array equal to original except zero-padded to an integral mulitple of blocks.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZE_BOOL

public static final int SIZE_BOOL
See Also:
Constant Field Values

SIZE_SHORT

public static final int SIZE_SHORT
See Also:
Constant Field Values

SIZE_INT

public static final int SIZE_INT
See Also:
Constant Field Values

SIZE_LONG

public static final int SIZE_LONG
See Also:
Constant Field Values

SIZE_DOUBLE

public static final int SIZE_DOUBLE
See Also:
Constant Field Values

digits

public static final char[] digits
A nibble->char mapping for printing out bytes.

Constructor Detail

ByteUtils

public ByteUtils()
Method Detail

qs_length

public static final int qs_length(QuickSerializable qs)
Returns the length of the byte array required to store a QuickSerializable object. This method calls qs.to_bytes, and therefore might be expensive.

Parameters:
qs - The QuickSerializable object to measure.
Returns:
the length of the byte array required to store qs.

qs_to_bytes

public static final byte[] qs_to_bytes(QuickSerializable qs)
Returns a byte array containing the serialized version of an object.

Parameters:
qs - The object to serialize
Returns:
An array of bytes, filled in by qs.to_bytes.

qs_clone

public static QuickSerializable qs_clone(QuickSerializable qs)
Clone a QuickSerializable object. We could just use the Object.clone function, but that would require that clone was implented properly, which is a potential source of bugs. So we just serialize and then deserialize it, instead.


bytesToInt

public static final int bytesToInt(byte[] data,
                                   int[] offset)
Return the int represented by the bytes in data staring at offset offset[0].

Parameters:
data - the array from which to read
offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
Returns:
the value of the int decoded

intToBytes

public static final void intToBytes(int i,
                                    byte[] data,
                                    int[] offset)
Write the bytes representing i into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

Parameters:
i - the int to encode
data - The byte array to store into, or null.
offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

bytesToShort

public static final short bytesToShort(byte[] data,
                                       int[] offset)
Return the short represented by the bytes in data staring at offset offset[0].

Parameters:
data - the array from which to read
offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
Returns:
the value of the short decoded

shortToBytes

public static final void shortToBytes(short s,
                                      byte[] data,
                                      int[] offset)
Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

Parameters:
s - the short to encode
data - The byte array to store into, or null.
offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

bytesToLong

public static final long bytesToLong(byte[] data,
                                     int[] offset)
Return the long represented by the bytes in data staring at offset offset[0].

Parameters:
data - the array from which to read
offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
Returns:
the value of the long decoded

longToBytes

public static final void longToBytes(long l,
                                     byte[] data,
                                     int[] offset)
Write the bytes representing l into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

Parameters:
l - the long to encode
data - The byte array to store into, or null.
offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

bytesToDouble

public static final double bytesToDouble(byte[] data,
                                         int[] offset)
Return the double represented by the bytes in data staring at offset offset[0].

Parameters:
data - the array from which to read
offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
Returns:
the value of the double decoded

doubleToBytes

public static final void doubleToBytes(double d,
                                       byte[] data,
                                       int[] offset)
Write the bytes representing d into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

Parameters:
d - the double to encode
data - The byte array to store into, or null.
offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

bytesToString

public static final String bytesToString(byte[] data,
                                         int[] offset)
Return the String represented by the bytes in data staring at offset offset[0]. This method relies on the user using the corresponding stringToBytes method to encode the String, so that it may properly retrieve the String length.

Parameters:
data - the array from which to read
offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
Returns:
the value of the String decoded

stringToBytes

public static final void stringToBytes(String s,
                                       byte[] data,
                                       int[] offset)
Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

Parameters:
s - the String to encode
data - The byte array to store into, or null.
offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

bytesToBool

public static final boolean bytesToBool(byte[] data,
                                        int[] offset)
Return the boolean represented by the bytes in data staring at offset offset[0].

Parameters:
data - the array from which to read
offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
Returns:
the value of the boolean decoded

boolToBytes

public static final void boolToBytes(boolean b,
                                     byte[] data,
                                     int[] offset)
Write the bytes representing b into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

Parameters:
b - the boolean to encode
data - The byte array to store into, or null.
offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

bytesToBigInteger

public static final BigInteger bytesToBigInteger(byte[] data,
                                                 int[] offset)
Return the BigInteger represented by the bytes in data staring at offset offset[0].

Parameters:
data - the array from which to read
offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
Returns:
the BigInteger decoded

bigIntegerToBytes

public static final void bigIntegerToBytes(BigInteger n,
                                           byte[] data,
                                           int[] offset)
Write the bytes representing n into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.

Parameters:
n - the BigInteger to encode
data - The byte array to store into, or null.
offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.

bytesToInts

public static final void bytesToInts(int[] dst,
                                     int dst_offset,
                                     byte[] src,
                                     int src_offset,
                                     int length)
Convert an array of bytess into an array of ints.

Parameters:
dst - the array to write
dst_offset - the start offset in dst, times 4. This measures the offset as if dst were an array of bytes (rather than ints).
src - the array to read
src_offset - the start offset in src
length - the number of bytes to copy.

intsToBytes

public static final void intsToBytes(byte[] dst,
                                     int dst_offset,
                                     int[] src,
                                     int src_offset,
                                     int length)
Convert an array of ints into an array of bytes.

Parameters:
dst - the array to write
dst_offset - the start offset in dst
src - the array to read
src_offset - the start offset in src, times 4. This measures the offset as if src were an array of bytes (rather than ints).
length - the number of bytes to copy.

byteToUnsignedInt

public static final int byteToUnsignedInt(byte b)
Convert a byte into an unsigned integer.

Parameters:
b - the byte to cast
Returns:
a postiive int whose lowest byte contains the bits of b.

memcpy

public static int memcpy(byte[] dst,
                         int dst_offset,
                         byte[] src,
                         int src_offset,
                         int length)
Copy contents of one array of bytes into another. If either array is null, simply return the length parameter directly.

Parameters:
dst - the array to write, or null
dst_offset - the start offset in dst
src - the array to read, or null
src_offset - the start offset in src
length - the number of bytes to copy.

memcmp

public static boolean memcmp(byte[] a,
                             int a_offset,
                             byte[] b,
                             int b_offset,
                             int length)
Compare the contents of one array of bytes to another.

Parameters:
a - the first array
a_offset - the start offset in a
b - the second array
b_offset - the start offset in b
length - the number of bytes to compare.

memclr

public static void memclr(byte[] array,
                          int offset,
                          int length)
Fill the given array with zeros.

Parameters:
array - the array to clear
offset - the start offset
length - the number of bytes to clear.

round_up

public static int round_up(int value,
                           int multiple)
Round a number up to a given multiple.

Parameters:
value - the number to be rounded
multiple - the number to which to be rounded
Returns:
the smallest int greater than or equal to value which divides multiple exactly.

zero_pad

public static byte[] zero_pad(byte[] original,
                              int block_size)
Return a new array equal to original except zero-padded to an integral mulitple of blocks. If the original is already an integral multiple of blocks, just return it.

Parameters:
original - the array of bytes to be padded
block_size - the size of the blocks
Returns:
an array whose size divides block_size exactly. The array is either original itself, or a copy whose first original.length bytes are equal to original.

equals

public static boolean equals(byte[] b1,
                             byte[] b2)
Determines whether two arrays of bytes contain the same contents.

Parameters:
b1 - The first array
b2 - The second array
Returns:
true if both arrays are null, both empty, or both of the same length with equal contents.

print_bytes

public static String print_bytes(byte[] data,
                                 int offset,
                                 int length)
Produce a String representation for the specified array of bytes. Print each byte as two hexadecimal digits.

Parameters:
data - The array to print
offset - the start offset in data
length - the number of bytes to print

print_bytes_exact

public static String print_bytes_exact(byte[] data,
                                       int offset,
                                       int length)

print_bytes

public static String print_bytes(byte[] data)