}
/**
* 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.
* <code>n=0134A0FF002EC0B1</code> returns the array
* <code>{ 01,34,A0,FF,00,2E,C0,B1 }</code>
*
* @param n the number
*
* @return the byte array representing the number.
*/
public static byte[] numberToByteArray(long n)
{
byte[] ba = new byte[8];
byte b;
// go through all bytes and filter them out
for (int s = 0; s < 8; s++) {
b = (byte) ((n >> ((8 - 1 - s) * 8)) & 0xFF);
ba[s] = b;
}
return ba;
}
/**
* 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.
*
* @param n the number
*
* @return the byte array representing the number.
*/
public static byte[] numberToByteArray(int n)
{
byte[] ba = new byte[4];
byte b;
// go through all bytes and filter them out
for (int s = 0; s < 4; s++) {
b = (byte) ((n >> ((4 - 1 - s) * 8)) & 0xFF);
ba[s] = b;
}
return ba;
}
/**
* 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.
*
* @param n the number
*
* @return the byte array representing the number.
*/
public static byte[] numberToByteArray(short n) {
byte[] ba = new byte[2];
byte b;
// go through all bytes and filter them out
for (int s = 0; s < 2; s++) {
b = (byte) ((n >> ((2 - 1 - s) * 8)) & 0xFF);
ba[s] = b;
}
return ba;
}
/**
* checks if two given byte block regions are equal or not.
*
* @param firstArray the first byte array.
* @param firstOffset the offset of the first block in the array.
* @param secondArray the second byte array.
* @param secondOffset the offset of the second block in the array.
* @param length the length of the blocks in the arrays.
*
* @return true, if both byte block regions are equal.
*/
public static boolean equalsRegion(
byte[] firstArray, int firstOffset,
byte[] secondArray, int secondOffset,
int length)
{
int firstIndex = firstOffset;
int secondIndex = secondOffset;
//-- compare byte blocks in arrays
while (firstIndex < firstArray.length
&& secondIndex < secondArray.length
&& firstIndex < firstOffset + length
&& secondIndex < secondOffset + length
&& firstArray[firstIndex] == secondArray[secondIndex]) {
firstIndex++;
secondIndex++;
}
//-- check if end reached, i.e. blocks are equal
if (firstIndex == firstOffset + length
&& secondIndex == secondOffset + length)
return true;
else
return false;
}
/**
* checks if two given byte regions are equal or not.
*
* @param firstArray the first byte array.
* @param firstOffset the offset of the first block in the array.
* @param secondArray the second byte array.
*
* @return true, if both byte block regions are equal.
*/
public static boolean equalsRegion(
byte[] firstArray, int firstOffset,
byte[] secondArray)
{
return equalsRegion(firstArray, firstOffset, secondArray, 0, secondArray.length);
}
/**
* compares two given byte block regions.
*
* @param firstArray the first byte array.
* @param firstOffset the offset of the first block in the array.
* @param secondArray the second byte array.
* @param secondOffset the offset of the second block in the array.
* @param length the length of the blocks in the arrays.
*
* @return -1, if first block < second block,
* 0, if first block == second block,
* 1, if first block > second block.
*/
public static int compareRegion(
byte[] firstArray, int firstOffset,
byte[] secondArray, int secondOffset,
int length)
{
int firstIndex = firstOffset;
int secondIndex = secondOffset;
//-- compare byte blocks in arrays
while (firstIndex < firstArray.length
&& secondIndex < secondArray.length
&& firstIndex < firstOffset + length
&& secondIndex < secondOffset + length) {
int res = compare(firstArray[firstIndex], secondArray[secondIndex]);
if (res != 0)
return res;
firstIndex++;
secondIndex++;
}
// end of blocks reached => blocks equal
return 0;
}
/**
* compares two bytes which are interpreted as unsigned bytes.
*
* @param first the first byte.
* @param second the second byte.
* @return -1, if first < second,
* 0, if first == second,
* 1, if first > second
*/
public static int compare(byte first, byte second)
{
int f = first & 0x000000FF;
int s = second & 0x000000FF;
if (f < s)
return -1;
else if (f == s)
return 0;
else
return 1;
}
/**
* returns a byte array containing the region of the source byte array.
*
* @param array the byte array.
* @param offset the offset of the region in the byte array.
* @param length the length of the region in the byte array.
*
* @return a byte array containing the region of the source byte array.
*/
public static byte[] getRegion(byte[] array, int offset, int length)
{
byte[] region = new byte[length];
// copy region into new byte array
for (int i = 0; i < length; i++)
region[i] = array[i + offset];
return region;
}
/**
* replaces the region of the first byte array by the
* bytes of the second byte array.
*
* @param firstArray the first byte array.
* @param offset the offset of the region in the first array.
* @param secondArray the second byte array.
*/
public static void replaceRegion(byte[] firstArray, int offset,
byte[] secondArray)
{
// copy region into byte array
for (int i = 0; i < secondArray.length; i++)
firstArray[i + offset] = secondArray[i];
}
/**
* replaces the region of a first byte array by the region
* of the second byte array.
*
* @param firstArray the first byte array.
* @param firstOffset the offset of the region in the first array.
* @param secondArray the second byte array.
* @param secondOffset the offset of the region in the second array.
* @param length the length of the region in the arrays.
*/
public static void replaceRegion(
byte[] firstArray, int firstOffset,
byte[] secondArray, int secondOffset, int length)
{
// copy region into byte array
for (int i = 0; i < length; i++)
firstArray[firstOffset + i] = secondArray[secondOffset + i];
}
/**
* transforms any String into a byte array using UTF8 encoding.
*
* @param string the string that has to be converted.
*
* @return the converted string as a byte array.
*/
public static byte[] stringConverter(String string){
byte[]defaultBytes=null;
byte[]utf8Bytes=null;
System.out.println(System.getProperty("file.encoding"));
String original = new String(string);
System.out.println("original = " + original);
System.out.println();
try {
utf8Bytes = original.getBytes("UTF8");
defaultBytes = original.getBytes();
String roundTrip = new String(utf8Bytes, "UTF8");
System.out.println("roundTrip = " + roundTrip);
System.out.println();
System.out.println();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return defaultBytes;
}
}