2015-04-11 23:52:45 +02:00
|
|
|
|
|
|
|
public class ArrayUtils
|
|
|
|
{
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* Return true, if the array part is sub array of src from offset
|
2015-04-28 22:47:56 +02:00
|
|
|
*
|
|
|
|
* @param src array
|
|
|
|
* @param part array
|
2015-04-13 22:42:16 +02:00
|
|
|
* @param offset int
|
|
|
|
* @return true, if part is completely a subarray of src on offset offset, else false
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public static boolean arrayIsPartOfOtherArrayOnOffset(byte[] src, byte[] part, int offset)
|
|
|
|
{
|
2015-04-28 22:47:56 +02:00
|
|
|
if (offset < 0 || part.length+offset > src.length) return false;
|
|
|
|
for (int i = 0; i < part.length; i++)
|
2015-04-11 23:52:45 +02:00
|
|
|
{
|
2015-04-28 22:47:56 +02:00
|
|
|
if (part[i] != src[i+offset]) return false;
|
2015-04-11 23:52:45 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* converts the byte array b into a char array by casting all the bytes into chars
|
2015-04-28 22:47:56 +02:00
|
|
|
*
|
2015-04-13 22:42:16 +02:00
|
|
|
* @param b byte array
|
|
|
|
* @return char array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public static char[] bytesToChars(byte[] b)
|
|
|
|
{
|
|
|
|
char[] c = new char[b.length];
|
2015-04-28 22:47:56 +02:00
|
|
|
for (int i = 0; i < b.length; i++)
|
2015-04-11 23:52:45 +02:00
|
|
|
c[i] = (char) b[i];
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* converts the char array into a byte array by casting all the chars into bytes
|
2015-04-28 22:47:56 +02:00
|
|
|
*
|
2015-04-13 22:42:16 +02:00
|
|
|
* @param c char array
|
|
|
|
* @return byte array
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public static byte[] charsToBytes(char[] c)
|
|
|
|
{
|
|
|
|
byte[] b = new byte[c.length];
|
2015-04-28 22:47:56 +02:00
|
|
|
for (int i = 0; i < c.length; i++)
|
2015-04-11 23:52:45 +02:00
|
|
|
b[i] = (byte) c[i];
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* concatenate two byte arrays
|
2015-04-28 22:47:56 +02:00
|
|
|
*
|
|
|
|
* @param first first byte array
|
2015-04-13 22:42:16 +02:00
|
|
|
* @param second second byte array
|
|
|
|
* @return first + second
|
|
|
|
*/
|
2015-04-28 22:47:56 +02:00
|
|
|
public static byte[] concatenate(byte[] first, byte[] second)
|
|
|
|
{
|
|
|
|
if (first == null) return second;
|
|
|
|
if (second == null) return first;
|
2015-04-13 22:42:16 +02:00
|
|
|
int aLen = first.length;
|
|
|
|
int bLen = second.length;
|
2015-04-11 23:52:45 +02:00
|
|
|
byte[] c = new byte[aLen+bLen];
|
2015-04-13 22:42:16 +02:00
|
|
|
System.arraycopy(first, 0, c, 0, aLen);
|
|
|
|
System.arraycopy(second, 0, c, aLen, bLen);
|
2015-04-11 23:52:45 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* convert an integer into a 32 bit byte array
|
2015-04-28 22:47:56 +02:00
|
|
|
*
|
2015-04-13 22:42:16 +02:00
|
|
|
* @param value integer
|
|
|
|
* @return byte array
|
|
|
|
*/
|
2015-04-28 22:47:56 +02:00
|
|
|
public static final byte[] intToByteArray(int value)
|
|
|
|
{
|
|
|
|
return new byte[]{
|
|
|
|
(byte) (value >>> 24),
|
|
|
|
(byte) (value >>> 16),
|
|
|
|
(byte) (value >>> 8),
|
|
|
|
(byte) value};
|
2015-04-11 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:42:16 +02:00
|
|
|
/**
|
|
|
|
* convert a signed byte array into an unsigned byte array (sort of)
|
2015-04-28 22:47:56 +02:00
|
|
|
*
|
2015-04-13 22:42:16 +02:00
|
|
|
* @param b byte array of signed bytes
|
|
|
|
* @return byte array of unsigned bytes
|
|
|
|
*/
|
2015-04-11 23:52:45 +02:00
|
|
|
public static byte[] unsign(byte[] b)
|
|
|
|
{
|
|
|
|
byte[] u = new byte[b.length];
|
2015-04-28 22:47:56 +02:00
|
|
|
for (int i = 0; i < b.length; i++)
|
|
|
|
u[i] = (byte) (b[i] & 0xFF);
|
2015-04-11 23:52:45 +02:00
|
|
|
return u;
|
|
|
|
}
|
|
|
|
}
|