org.fosstrak.hal.util
Class ByteBlock

java.lang.Object
  extended by org.fosstrak.hal.util.ByteBlock

public class ByteBlock
extends java.lang.Object

ByteBlock represents a block of bytes of arbitrary length. The bytes are stored internally in a byte array, howerver the bytes are numbered from left to right in contrary to numbers in hexadecimal format. The bytes in the array are also considered unsigned in contrast to Java. This allows the usage of ByteBlock to store output of hardware devices. The following is an example of the String representation of a byte block of length 12: 10FF024E6A0000A1611D6400. In addition the class provides several static byte array support functions, which allow the usage of ByteBlock without creating objects of the class.

Version:
1.0, 03/2003
Author:
Matthias Lampe, lampe@acm.org

Field Summary
protected  byte[] bytes
          the byte array to store the byte block
 
Constructor Summary
ByteBlock(byte[] bytes)
          creates a byte block from the given byte array.
ByteBlock(java.lang.String bytes)
          creates a byte block from the given bytes as a String.
 
Method Summary
static java.lang.String byteArrayToHexString(byte[] byteArray)
          returns a string with the bytes of the input array in hexadecimal representation.
static java.lang.String byteArrayToHexString(byte[] byteArray, int length)
          returns a string with the byte of the input array in hexadecimal representation trimed/padded to a specific length.
static int bytesToNumber(byte[] b)
          converts an unsigned byte array (MSB first, max first four bytes) to a (signed) integer.
static java.lang.String byteToHexString(byte b)
          returns a string with the byte in hexadecimal representation.
static int byteToNumber(byte b)
          converts an unsigned byte value to a (signed) integer.
 java.lang.Object clone()
          clones the byte block.
static int compare(byte first, byte second)
          compares two bytes which are interpreted as unsigned bytes.
static int compareRegion(byte[] firstArray, int firstOffset, byte[] secondArray, int secondOffset, int length)
          compares two given byte block regions.
 boolean equals(java.lang.Object o)
          checks if two byte blocks are equal or not.
static boolean equalsRegion(byte[] firstArray, int firstOffset, byte[] secondArray)
          checks if two given byte regions are equal or not.
static boolean equalsRegion(byte[] firstArray, int firstOffset, byte[] secondArray, int secondOffset, int length)
          checks if two given byte block regions are equal or not.
 byte[] getInternalByteArray()
          returns a reference to the internal byte array.
static byte[] getRegion(byte[] array, int offset, int length)
          returns a byte array containing the region of the source byte array.
 int hashCode()
          calculates a hashcode for the byte block.
static byte[] hexStringToByteArray(java.lang.String hexString)
          converts a String of hex bytes to a byte array.
static byte[] hexStringToByteArray(java.lang.String hexString, int length)
          converts a String of hex byte to a byte array of a specific length.
static byte[] numberToByteArray(int n)
          creates a byte array out of the given number.
static byte[] numberToByteArray(long n)
          creates a byte array out of the given number.
static byte[] numberToByteArray(short n)
          creates a byte array out of the given number.
static void replaceRegion(byte[] firstArray, int offset, byte[] secondArray)
          replaces the region of the first byte array by the bytes of the second byte array.
static void replaceRegion(byte[] firstArray, int firstOffset, byte[] secondArray, int secondOffset, int length)
          replaces the region of a first byte array by the region of the second byte array.
static byte[] stringConverter(java.lang.String string)
          transforms any String into a byte array using UTF8 encoding.
 byte[] toByteArray()
          gets the byte block as a byte array.
 java.lang.String toString()
          gets the byte block as a String.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

bytes

protected byte[] bytes
the byte array to store the byte block

Constructor Detail

ByteBlock

public ByteBlock(byte[] bytes)
creates a byte block from the given byte array.

Parameters:
bytes - the byte array, which is copied into the internal representation.

ByteBlock

public ByteBlock(java.lang.String bytes)
creates a byte block from the given bytes as a String.

Parameters:
bytes - the bytes as a String.
Method Detail

getInternalByteArray

public byte[] getInternalByteArray()
returns a reference to the internal byte array.

Returns:
the internal byte array.

toByteArray

public byte[] toByteArray()
gets the byte block as a byte array.


toString

public java.lang.String toString()
gets the byte block as a String.

Overrides:
toString in class java.lang.Object
Returns:
the unsigned hex number as a String.

clone

public java.lang.Object clone()
clones the byte block.

Overrides:
clone in class java.lang.Object
Returns:
the deep copy of the byte block.

equals

public boolean equals(java.lang.Object o)
checks if two byte blocks are equal or not.

Overrides:
equals in class java.lang.Object
Returns:
true, if both byte blocks are equal.
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
calculates a hashcode for the byte block.

Overrides:
hashCode in class java.lang.Object
Returns:
the hashcode of the byte block.
See Also:
Object.hashCode()

byteToHexString

public static java.lang.String byteToHexString(byte b)
returns a string with the byte in hexadecimal representation. The byte is interpreted as unsigned value.

Parameters:
b - the byte.
Returns:
a string with the byte in hexadecimal representation.

byteArrayToHexString

public static java.lang.String byteArrayToHexString(byte[] byteArray)
returns a string with the bytes of the input array in hexadecimal representation.

Parameters:
byteArray - the array of bytes.
Returns:
a string with the bytes of the input array in hexadecimal representation.

byteArrayToHexString

public static java.lang.String byteArrayToHexString(byte[] byteArray,
                                                    int length)
returns a string with the byte of the input array in hexadecimal representation trimed/padded to a specific length. If length is less than the length of the input array, the most significant bits (low array indices) are cut. If length is greater than the length of the input array, "00" bytes are added in front of the resulting string.

Parameters:
byteArray -
length -
Returns:
a string with the bytes of the input array in hexadecimal representation.

hexStringToByteArray

public static byte[] hexStringToByteArray(java.lang.String hexString)
converts a String of hex bytes to a byte array.

Parameters:
hexString - the byte array as a String.

hexStringToByteArray

public static byte[] hexStringToByteArray(java.lang.String hexString,
                                          int length)
converts a String of hex byte to a byte array of a specific length. If length is less than the converted String, the most significant bits (low array indices) are cut. If length is greater than the converted String, the resulting array is padded with 0 bytes at the beginning (low array indices).

Parameters:
hexString -
length -
Returns:
a byte array derived from the hexadecimal representation given

byteToNumber

public static int byteToNumber(byte b)
converts an unsigned byte value to a (signed) integer.

Parameters:
b - Unsigned byte value
Returns:
Signed integer

bytesToNumber

public static int bytesToNumber(byte[] b)
converts an unsigned byte array (MSB first, max first four bytes) to a (signed) integer.

Parameters:
b -
Returns:
the value representated by the input byte array

numberToByteArray

public static byte[] numberToByteArray(long n)
creates a byte array out of the given number. The numbering of the bytes starts from the left to the right in contrary to usual numbering e.g. n=0134A0FF002EC0B1 returns the array { 01,34,A0,FF,00,2E,C0,B1 }

Parameters:
n - the number
Returns:
the byte array representing the number.

numberToByteArray

public static byte[] numberToByteArray(int n)
creates a byte array out of the given number. The numbering of the bytes starts from the left to the right in contrary to usual numbering.

Parameters:
n - the number
Returns:
the byte array representing the number.

numberToByteArray

public static byte[] numberToByteArray(short n)
creates a byte array out of the given number. The numbering of the bytes starts from the left to the right in contrary to usual numbering.

Parameters:
n - the number
Returns:
the byte array representing the number.

equalsRegion

public static boolean equalsRegion(byte[] firstArray,
                                   int firstOffset,
                                   byte[] secondArray,
                                   int secondOffset,
                                   int length)
checks if two given byte block regions are equal or not.

Parameters:
firstArray - the first byte array.
firstOffset - the offset of the first block in the array.
secondArray - the second byte array.
secondOffset - the offset of the second block in the array.
length - the length of the blocks in the arrays.
Returns:
true, if both byte block regions are equal.

equalsRegion

public static boolean equalsRegion(byte[] firstArray,
                                   int firstOffset,
                                   byte[] secondArray)
checks if two given byte regions are equal or not.

Parameters:
firstArray - the first byte array.
firstOffset - the offset of the first block in the array.
secondArray - the second byte array.
Returns:
true, if both byte block regions are equal.

compareRegion

public static int compareRegion(byte[] firstArray,
                                int firstOffset,
                                byte[] secondArray,
                                int secondOffset,
                                int length)
compares two given byte block regions.

Parameters:
firstArray - the first byte array.
firstOffset - the offset of the first block in the array.
secondArray - the second byte array.
secondOffset - the offset of the second block in the array.
length - the length of the blocks in the arrays.
Returns:
-1, if first block < second block, 0, if first block == second block, 1, if first block > second block.

compare

public static int compare(byte first,
                          byte second)
compares two bytes which are interpreted as unsigned bytes.

Parameters:
first - the first byte.
second - the second byte.
Returns:
-1, if first < second, 0, if first == second, 1, if first > second

getRegion

public static byte[] getRegion(byte[] array,
                               int offset,
                               int length)
returns a byte array containing the region of the source byte array.

Parameters:
array - the byte array.
offset - the offset of the region in the byte array.
length - the length of the region in the byte array.
Returns:
a byte array containing the region of the source byte array.

replaceRegion

public static void replaceRegion(byte[] firstArray,
                                 int offset,
                                 byte[] secondArray)
replaces the region of the first byte array by the bytes of the second byte array.

Parameters:
firstArray - the first byte array.
offset - the offset of the region in the first array.
secondArray - the second byte array.

replaceRegion

public static void replaceRegion(byte[] firstArray,
                                 int firstOffset,
                                 byte[] secondArray,
                                 int secondOffset,
                                 int length)
replaces the region of a first byte array by the region of the second byte array.

Parameters:
firstArray - the first byte array.
firstOffset - the offset of the region in the first array.
secondArray - the second byte array.
secondOffset - the offset of the region in the second array.
length - the length of the region in the arrays.

stringConverter

public static byte[] stringConverter(java.lang.String string)
transforms any String into a byte array using UTF8 encoding.

Parameters:
string - the string that has to be converted.
Returns:
the converted string as a byte array.


Copyright © 2008. All Rights Reserved.