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; 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,23 +266,20 @@ 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 EMPTY_BYTE_ARRAY;
firstNonZero = i;
break; case 0:
} return data;
default:
byte[] result = new byte[data.length - firstNonZero];
System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
return result;
} }
if (i == data.length)
return new byte[1];
if (firstNonZero == 0)
return data;
byte[] result = new byte[data.length - firstNonZero];
System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
return result;
} }
/** /**

View File

@ -32,30 +32,31 @@ 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();
} }
public DataWord(String data){ public DataWord(String data) {
this(Hex.decode(data)); this(Hex.decode(data));
} }
public DataWord(byte[] data) { public DataWord(byte[] data) {
if (data == null) if (data == null)
this.data = ByteUtil.EMPTY_BYTE_ARRAY; this.data = ByteUtil.EMPTY_BYTE_ARRAY;
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
@ -316,8 +315,8 @@ public class DataWord implements Comparable<DataWord> {
if (firstNonZero == -1) return 0; if (firstNonZero == -1) return 0;
return 31 - firstNonZero + 1; return 31 - firstNonZero + 1;
} }
public boolean isHex(String hex){ public boolean isHex(String hex) {
return Hex.toHexString(data).equals(hex); return Hex.toHexString(data).equals(hex);
} }
} }

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