minor fixes around HexString
- fix buffer size calculation
- slightly improve hex conversino logic
- remove unnecessary dependency
Change-Id: I779d88d0c5ec1c4999ce6237d0c17c3057581ea7
diff --git a/utils/misc/pom.xml b/utils/misc/pom.xml
index 24adf2d..bd9040c 100644
--- a/utils/misc/pom.xml
+++ b/utils/misc/pom.xml
@@ -70,11 +70,6 @@
<artifactId>commons-lang3</artifactId>
</dependency>
- <!-- TODO: do we still need this here? -->
- <dependency>
- <groupId>com.eclipsesource.minimal-json</groupId>
- <artifactId>minimal-json</artifactId>
- </dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
diff --git a/utils/misc/src/main/java/org/onlab/util/HexString.java b/utils/misc/src/main/java/org/onlab/util/HexString.java
index f7712d6..2f8a679 100644
--- a/utils/misc/src/main/java/org/onlab/util/HexString.java
+++ b/utils/misc/src/main/java/org/onlab/util/HexString.java
@@ -17,6 +17,8 @@
public final class HexString {
+ private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
+
private HexString() {
}
@@ -45,18 +47,15 @@
if (separator == null) {
separator = "";
}
- int i;
- StringBuilder ret = new StringBuilder(bytes.length * 3 - 1);
- String tmp;
- for (i = 0; i < bytes.length; i++) {
- if (i > 0) {
+ int slen = bytes.length * (2 + separator.length()) - separator.length();
+ StringBuilder ret = new StringBuilder(slen);
+ boolean addSeparator = !separator.isEmpty();
+ for (int i = 0; i < bytes.length; i++) {
+ if (i > 0 && addSeparator) {
ret.append(separator);
}
- tmp = Integer.toHexString((bytes[i] & 0xff));
- if (tmp.length() == 1) {
- ret.append('0');
- }
- ret.append(tmp);
+ ret.append(HEX_CHARS[(bytes[i] >> 4) & 0xF]);
+ ret.append(HEX_CHARS[(bytes[i] & 0xF)]);
}
return ret.toString();
}
diff --git a/utils/misc/src/test/java/org/onlab/util/HexStringTest.java b/utils/misc/src/test/java/org/onlab/util/HexStringTest.java
index d69ae1e..1060a73 100644
--- a/utils/misc/src/test/java/org/onlab/util/HexStringTest.java
+++ b/utils/misc/src/test/java/org/onlab/util/HexStringTest.java
@@ -17,14 +17,9 @@
import org.junit.Test;
-import com.esotericsoftware.minlog.Log;
-
-import junit.framework.TestCase;
+import static org.junit.Assert.*;
import java.nio.ByteBuffer;
-import java.util.Arrays;
-
-import static org.junit.Assert.fail;
/**
* Test of the Hexstring.
@@ -38,7 +33,7 @@
String dpidStr = "00:00:00:23:20:2d:16:71";
long dpid = HexString.toLong(dpidStr);
String testStr = HexString.toHexString(dpid);
- TestCase.assertEquals(dpidStr, testStr);
+ assertEquals(dpidStr, testStr);
}
@Test
@@ -46,7 +41,7 @@
String dpidStr = "3e:1f:01:fc:72:8c:63:31";
long valid = 0x3e1f01fc728c6331L;
long testLong = HexString.toLong(dpidStr);
- TestCase.assertEquals(valid, testLong);
+ assertEquals(valid, testLong);
}
@Test
@@ -54,7 +49,7 @@
String dpidStr = "ca:7c:5e:d1:64:7a:95:9b";
long valid = -3856102927509056101L;
long testLong = HexString.toLong(dpidStr);
- TestCase.assertEquals(valid, testLong);
+ assertEquals(valid, testLong);
}
@Test
@@ -67,21 +62,17 @@
byte[] testBytesNoSep = HexString.fromHexString(dpidStrNoSep, null);
byte[] testBytesUCase = HexString.fromHexString(dpidStr.toUpperCase());
byte[] testBytesUCaseNoSep = HexString.fromHexString(dpidStrNoSep.toUpperCase(), null);
- TestCase.assertTrue(Arrays.equals(validBytes, testBytes));
- TestCase.assertTrue(Arrays.equals(validBytes, testBytesNoSep));
- TestCase.assertTrue(Arrays.equals(validBytes, testBytesUCase));
- TestCase.assertTrue(Arrays.equals(validBytes, testBytesUCaseNoSep));
+ assertArrayEquals(validBytes, testBytes);
+ assertArrayEquals(validBytes, testBytesNoSep);
+ assertArrayEquals(validBytes, testBytesUCase);
+ assertArrayEquals(validBytes, testBytesUCaseNoSep);
}
- @Test
+ @Test(expected = NumberFormatException.class)
public void testToLongError() {
String dpidStr = "09:08:07:06:05:04:03:02:01";
- try {
- HexString.toLong(dpidStr);
- fail("HexString.toLong() should have thrown a NumberFormatException");
- } catch (NumberFormatException expected) {
- Log.info("HexString.toLong() have thrown a NumberFormatException");
- }
+ HexString.toLong(dpidStr);
+ fail("HexString.toLong() should have thrown a NumberFormatException");
}
@Test
@@ -89,18 +80,17 @@
byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 };
String valid = "00:00:00:00:00:00:00:ff";
String testString = HexString.toHexString(dpid);
- TestCase.assertEquals(valid, testString);
+ assertEquals(valid, testString);
+
+ String validNoSep = "00000000000000ff";
+ assertEquals(validNoSep, HexString.toHexString(dpid, null));
}
- @Test
+ @Test(expected = NumberFormatException.class)
public void testFromHexStringError() {
String invalidStr = "00:00:00:00:00:00:ffff";
- try {
- HexString.fromHexString(invalidStr);
- fail("HexString.fromHexString() should have thrown a NumberFormatException");
- } catch (NumberFormatException expected) {
- Log.info("HexString.toLong() have thrown a NumberFormatException");
- }
+ HexString.fromHexString(invalidStr);
+ fail("HexString.fromHexString() should have thrown a NumberFormatException");
}
}