org.fosstrak.hal
Class UnsignedByteArray

java.lang.Object
  extended by org.fosstrak.hal.UnsignedByteArray

public class UnsignedByteArray
extends java.lang.Object

UnsignedByteArray represents an array of unsigned bytes of arbitrary length. Since Java does not support unsigned byte arrays this class can be used to send or receive bytes from hardware devices such as RFID readers. In addition, the class provides several static byte array support functions, which allow the usage of UnsignedByteArray without creating objects of the class.

Author:
Matthias Lampe, lampe@acm.org

Field Summary
protected  byte[] bytes
          The internal representation of the UnsignedByteArray.
 
Constructor Summary
UnsignedByteArray(byte[] bytes)
          creates an UnsignedByteArray from the given Java byte array.
UnsignedByteArray(java.lang.String bytes)
          creates an UnsignedByteArray from a String representation.
 
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 byteToHexString(byte b)
          returns a string with the byte in hexadecimal representation.
 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 UnsignedByteArray 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 UnsignedByteArray regions are equal or not.
static boolean equalsRegion(byte[] firstArray, int firstOffset, byte[] secondArray, int secondOffset, int length)
          checks if two given UnsignedByteArray regions are equal or not.
protected  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[] numberToByteArray(int n)
          creates a byte array out of the given int number.
static byte[] numberToByteArray(long n)
          creates a byte array out of the given long number.
static byte[] numberToByteArray(short n)
          creates a byte array out of the given short 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 UnsignedByteArray as a Java byte array to be interpreted as unsigned bytes.
 java.lang.String toString()
          gets the UnsignedByteArray as a hex number String representation.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

bytes

protected byte[] bytes
The internal representation of the UnsignedByteArray. Each byte in the byte array is interpreted as unsigned during operations on the array.

Constructor Detail

UnsignedByteArray

public UnsignedByteArray(byte[] bytes)
creates an UnsignedByteArray from the given Java byte array. The bytes in the given byte array are interpreted as unsigned bytes.

Parameters:
bytes - the byte array, which is interpreted as unsigned bytes

UnsignedByteArray

public UnsignedByteArray(java.lang.String bytes)
creates an UnsignedByteArray from a String representation.

Parameters:
bytes - the bytes in hex format as a String
Method Detail

getInternalByteArray

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

Returns:
the internal byte array.

toByteArray

public byte[] toByteArray()
gets the UnsignedByteArray as a Java byte array to be interpreted as unsigned bytes.

Returns:
a byte array representing the UnsignedByteArray.

toString

public java.lang.String toString()
gets the UnsignedByteArray as a hex number String representation.

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

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 interpreted as unsigned bytes.
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. The hex bytes are interpreted as unsigned bytes.

Parameters:
hexString - the byte array as a String.

numberToByteArray

public static byte[] numberToByteArray(long n)
creates a byte array out of the given long 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 }. The bytes in the array should be interpreted as unsigned bytes.

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 int number. The numbering of the bytes starts from the left to the right in contrary to usual numbering. The bytes in the array should be interpreted as unsigned bytes.

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 short number. The numbering of the bytes starts from the left to the right in contrary to usual numbering. The bytes in the array should be interpreted as unsigned bytes.

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 UnsignedByteArray regions are equal or not.

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

equalsRegion

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

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

compareRegion

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

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

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.