speed improvements and add test-cases

This commit is contained in:
ligi 2015-01-10 16:25:40 +01:00
parent 910a8b9552
commit 9ab5c21fdd
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; int length = a.length < b.length ? a.length : b.length;
while (i < length) { while (i < length) {
if (a[i] != b[i]) if (a[i] != b[i])
break; return i;
i++; i++;
} }
return i; return i;
@ -122,12 +122,11 @@ public class ByteUtil {
*/ */
public static byte[] calcPacketLength(byte[] msg) { public static byte[] calcPacketLength(byte[] msg) {
int msgLen = msg.length; int msgLen = msg.length;
byte[] len = { return new byte[]{
(byte) ((msgLen >> 24) & 0xFF), (byte) ((msgLen >> 24) & 0xFF),
(byte) ((msgLen >> 16) & 0xFF), (byte) ((msgLen >> 16) & 0xFF),
(byte) ((msgLen >> 8) & 0xFF), (byte) ((msgLen >> 8) & 0xFF),
(byte) ((msgLen) & 0xFF)}; (byte) ((msgLen) & 0xFF)};
return len;
} }
/** /**
@ -254,15 +253,12 @@ public class ByteUtil {
} }
public static int firstNonZeroByte(byte[] data) { public static int firstNonZeroByte(byte[] data) {
int firstNonZero = -1; for (int i = 0; i < data.length; ++i) {
int i = 0;
for (; i < data.length; ++i) {
if (data[i] != 0) { if (data[i] != 0) {
firstNonZero = i; return i;
break;
} }
} }
return firstNonZero; return -1;
} }
public static byte[] stripLeadingZeroes(byte[] data) { public static byte[] stripLeadingZeroes(byte[] data) {
@ -270,24 +266,21 @@ public class ByteUtil {
if (data == null) if (data == null)
return null; return null;
int firstNonZero = firstNonZeroByte(data); final int firstNonZero = firstNonZeroByte(data);
int i = 0; switch (firstNonZero) {
for (; i < data.length; ++i) { case -1:
if (data[i] != 0) { return new byte[0];
firstNonZero = i;
break; case 0:
}
}
if (i == data.length)
return new byte[1];
if (firstNonZero == 0)
return data; return data;
default:
byte[] result = new byte[data.length - firstNonZero]; byte[] result = new byte[data.length - firstNonZero];
System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero); System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
return result; return result;
} }
}
/** /**
* increment byte array as a number until max is reached * 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) { public DataWord(int num) {
ByteBuffer bInt = ByteBuffer.allocate(4).putInt(num); this(ByteBuffer.allocate(4).putInt(num));
ByteBuffer data = ByteBuffer.allocate(32);
System.arraycopy(bInt.array(), 0, data.array(), 28, 4);
this.data = data.array();
} }
public DataWord(long num) { public DataWord(long num) {
ByteBuffer bLong = ByteBuffer.allocate(8).putLong(num); this(ByteBuffer.allocate(8).putLong(num));
ByteBuffer data = ByteBuffer.allocate(32); }
System.arraycopy(bLong.array(), 0, data.array(), 24, 8);
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(); this.data = data.array();
} }
@ -55,7 +56,7 @@ public class DataWord implements Comparable<DataWord> {
else if (data.length <= 32) else if (data.length <= 32)
System.arraycopy(data, 0, this.data, 32 - data.length, data.length); System.arraycopy(data, 0, this.data, 32 - data.length, data.length);
else 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() { public byte[] getData() {
@ -107,11 +108,10 @@ public class DataWord implements Comparable<DataWord> {
} }
public boolean isZero() { public boolean isZero() {
byte result = 0;
for (byte tmp : data) { for (byte tmp : data) {
result |= tmp; if (tmp != 0) return false;
} }
return result == 0; return true;
} }
// only in case of signed operation // only in case of signed operation
@ -282,9 +282,8 @@ public class DataWord implements Comparable<DataWord> {
DataWord dataWord = (DataWord) o; 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 @Override

View File

@ -100,11 +100,21 @@ public class ByteUtilTest {
@Test @Test
public void testStripLeadingZeroes() { public void testStripLeadingZeroes() {
byte[] test1 = new byte[]{0x00, 0x01}; byte[] test1 = null;
byte[] test2 = new byte[]{0x00, 0x00, 0x01}; byte[] test2 = new byte[]{};
byte[] expected = new byte[]{0x01}; byte[] test3 = new byte[]{0x00};
assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test1)); byte[] test4 = new byte[]{0x00, 0x01};
assertArrayEquals(expected, ByteUtil.stripLeadingZeroes(test2)); 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 @Test