Merge remote-tracking branch 'origin/master'

This commit is contained in:
Roman Mandeleil 2015-01-14 11:47:58 +02:00
commit e56dfacaae
3 changed files with 49 additions and 47 deletions

View File

@ -83,7 +83,7 @@ public class ByteUtil {
int length = a.length < b.length ? a.length : b.length;
while (i < length) {
if (a[i] != b[i])
break;
return i;
i++;
}
return i;
@ -122,12 +122,11 @@ public class ByteUtil {
*/
public static byte[] calcPacketLength(byte[] msg) {
int msgLen = msg.length;
byte[] len = {
return new byte[]{
(byte) ((msgLen >> 24) & 0xFF),
(byte) ((msgLen >> 16) & 0xFF),
(byte) ((msgLen >> 8) & 0xFF),
(byte) ((msgLen) & 0xFF)};
return len;
}
/**
@ -254,15 +253,12 @@ public class ByteUtil {
}
public static int firstNonZeroByte(byte[] data) {
int firstNonZero = -1;
int i = 0;
for (; i < data.length; ++i) {
for (int i = 0; i < data.length; ++i) {
if (data[i] != 0) {
firstNonZero = i;
break;
return i;
}
}
return firstNonZero;
return -1;
}
public static byte[] stripLeadingZeroes(byte[] data) {
@ -270,24 +266,21 @@ public class ByteUtil {
if (data == null)
return null;
int firstNonZero = firstNonZeroByte(data);
int i = 0;
for (; i < data.length; ++i) {
if (data[i] != 0) {
firstNonZero = i;
break;
}
}
if (i == data.length)
return new byte[1];
if (firstNonZero == 0)
final int firstNonZero = firstNonZeroByte(data);
switch (firstNonZero) {
case -1:
return EMPTY_BYTE_ARRAY;
case 0:
return data;
default:
byte[] result = new byte[data.length - firstNonZero];
System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
return result;
}
}
/**
* increment byte array as a number until max is reached

View File

@ -32,16 +32,17 @@ public class DataWord implements Comparable<DataWord> {
}
public DataWord(int num) {
ByteBuffer bInt = ByteBuffer.allocate(4).putInt(num);
ByteBuffer data = ByteBuffer.allocate(32);
System.arraycopy(bInt.array(), 0, data.array(), 28, 4);
this.data = data.array();
this(ByteBuffer.allocate(4).putInt(num));
}
public DataWord(long num) {
ByteBuffer bLong = ByteBuffer.allocate(8).putLong(num);
ByteBuffer data = ByteBuffer.allocate(32);
System.arraycopy(bLong.array(), 0, data.array(), 24, 8);
this(ByteBuffer.allocate(8).putLong(num));
}
private DataWord(ByteBuffer buffer) {
final ByteBuffer data = ByteBuffer.allocate(32);
final byte[] array = buffer.array();
System.arraycopy(array, 0, data.array(), 32 - array.length, array.length);
this.data = data.array();
}
@ -55,7 +56,7 @@ public class DataWord implements Comparable<DataWord> {
else if (data.length <= 32)
System.arraycopy(data, 0, this.data, 32 - data.length, data.length);
else
throw new RuntimeException("Data word can't exit 32 bytes: " + data);
throw new RuntimeException("Data word can't exceed 32 bytes: " + data);
}
public byte[] getData() {
@ -107,11 +108,10 @@ public class DataWord implements Comparable<DataWord> {
}
public boolean isZero() {
byte result = 0;
for (byte tmp : data) {
result |= tmp;
if (tmp != 0) return false;
}
return result == 0;
return true;
}
// only in case of signed operation
@ -282,9 +282,8 @@ public class DataWord implements Comparable<DataWord> {
DataWord dataWord = (DataWord) o;
if (!java.util.Arrays.equals(data, dataWord.data)) return false;
return java.util.Arrays.equals(data, dataWord.data);
return true;
}
@Override

View File

@ -100,11 +100,21 @@ public class ByteUtilTest {
@Test
public void testStripLeadingZeroes() {
byte[] test1 = new byte[]{0x00, 0x01};
byte[] test2 = new byte[]{0x00, 0x00, 0x01};
byte[] expected = new byte[]{0x01};
assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test1));
assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test2));
byte[] test1 = null;
byte[] test2 = new byte[]{};
byte[] test3 = new byte[]{0x00};
byte[] test4 = new byte[]{0x00, 0x01};
byte[] test5 = new byte[]{0x00, 0x00, 0x01};
byte[] expected1 = null;
byte[] expected2 = new byte[]{};
byte[] expected3 = new byte[]{};
byte[] expected4 = new byte[]{0x01};
byte[] expected5 = new byte[]{0x01};
assertArrayEquals(expected1, ByteUtil.stripLeadingZeroes(test1));
assertArrayEquals(expected2, ByteUtil.stripLeadingZeroes(test2));
assertArrayEquals(expected3, ByteUtil.stripLeadingZeroes(test3));
assertArrayEquals(expected4, ByteUtil.stripLeadingZeroes(test4));
assertArrayEquals(expected5, ByteUtil.stripLeadingZeroes(test5));
}
@Test