peakml.util
Class ByteArray

java.lang.Object
  extended by peakml.util.ByteArray

public class ByteArray
extends java.lang.Object

Utility functions for converting arrays of basic types into an array of bytes and vica-versa, which is especially useful for storing large amounts of data into a binary file. This provides a central point for platform independent storage of data. The endiannes can be set for each conversion in order to support platform independent conversion. Additionally, each conversion function accepts a precision value, indicating the amount of bits to use for storage (ie either 32 or 64).


Field Summary
static int ENDIAN_BIG
          The data is stored in big endian mode.
static int ENDIAN_LITTLE
          The data is stored in little endian mode.
 
Constructor Summary
ByteArray()
           
 
Method Summary
static byte[] toByteArray(double[] array, int endiannes, int precision)
          Converts the given double array into a byte-array.
static byte[] toByteArray(int[] array, int endiannes, int precision)
          Converts the given integer array into a byte-array.
static byte[] toByteArray(long[] array, int endiannes, int precision)
          Converts the given long array into a byte-array.
static double[] toDoubleArray(byte[] array, int endiannes, int precision)
          Converts a byte array containing double-values into a double array.
static int[] toIntArray(byte[] array, int endiannes, int precision)
          Converts a byte array containing integer-values into a long array.
static long[] toLongArray(byte[] array, int endiannes, int precision)
          Converts a byte array containing long-values into a long array.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENDIAN_BIG

public static final int ENDIAN_BIG
The data is stored in big endian mode.

See Also:
Constant Field Values

ENDIAN_LITTLE

public static final int ENDIAN_LITTLE
The data is stored in little endian mode.

See Also:
Constant Field Values
Constructor Detail

ByteArray

public ByteArray()
Method Detail

toByteArray

public static byte[] toByteArray(int[] array,
                                 int endiannes,
                                 int precision)
Converts the given integer array into a byte-array. The given value for endiannes (either ENDIAN_BIG or ENDIAN_LITTLE) indicates the byte-order the data should be stored in. The precision indicates the number of bits for each element in the integer array.

For the precision only 32 is supported, as 64 does not make sense on the Java platform.

Parameters:
array - The array with the integer-values.
endiannes - The byte-order in which the data has been stored (either ENDIAN_BIG or ENDIAN_LITTLE).
precision - The number of bits for each element in the array.
Returns:
The array with the byte-values.

toByteArray

public static byte[] toByteArray(long[] array,
                                 int endiannes,
                                 int precision)
Converts the given long array into a byte-array. The given value for endiannes (either ENDIAN_BIG or ENDIAN_LITTLE) indicates the byte-order the data should be stored in. The precision indicates the number of bits for each element in the long array.

Parameters:
array - The array with the long-values.
endiannes - The byte-order in which the data has been stored (either ENDIAN_BIG or ENDIAN_LITTLE).
precision - The number of bits for each element in the array.
Returns:
The array with the byte-values.

toByteArray

public static byte[] toByteArray(double[] array,
                                 int endiannes,
                                 int precision)
Converts the given double array into a byte-array. The given value for endiannes (either ENDIAN_BIG or ENDIAN_LITTLE) indicates the byte-order the data should be stored in. The precision indicates the number of bits for each element in the integer array.

Parameters:
array - The array with the double-values.
endiannes - The byte-order in which the data has been stored (either ENDIAN_BIG or ENDIAN_LITTLE).
precision - The number of bits for each element in the array.
Returns:
The array with the byte-values.

toIntArray

public static int[] toIntArray(byte[] array,
                               int endiannes,
                               int precision)
Converts a byte array containing integer-values into a long array. The given value for endiannes (either ENDIAN_BIG or ENDIAN_LITTLE) indicates the byte-order the data has been stored in. The precision indicates the number of bits for each element in the integer array.

For the precision only 32 is supported, as 64 does not make sense on the Java platform.

Parameters:
array - The byte-array to be converted.
endiannes - The byte-order in which the data has been stored (either ENDIAN_BIG or ENDIAN_LITTLE).
precision - The number of bits for each element in the long-array.
Returns:
The array with the integer-values.

toLongArray

public static long[] toLongArray(byte[] array,
                                 int endiannes,
                                 int precision)
Converts a byte array containing long-values into a long array. The given value for endiannes (either ENDIAN_BIG or ENDIAN_LITTLE) indicates the byte-order the data has been stored in. The precision indicates the number of bits for each element in the long array.

Parameters:
array - The byte-array to be converted.
endiannes - The byte-order in which the data has been stored (either ENDIAN_BIG or ENDIAN_LITTLE).
precision - The number of bits for each element in the long-array.
Returns:
The array with the long-values.

toDoubleArray

public static double[] toDoubleArray(byte[] array,
                                     int endiannes,
                                     int precision)
Converts a byte array containing double-values into a double array. The given value for endiannes (either ENDIAN_BIG or ENDIAN_LITTLE) indicates the byte-order the data has been stored in. The precision indicates the number of bits for each element in the double array.

Parameters:
array - The byte-array to be converted.
endiannes - The byte-order in which the data has been stored (either ENDIAN_BIG or ENDIAN_LITTLE).
precision - The number of bits for each element in the double-array.
Returns:
The array with the double-values.