Commit MetaInfo

Revision0c0f47d16b5f3544eb9304dd46b694d6ec3d23d3 (tree)
Time2012-11-19 19:35:39
Authorjwat <jwat@user...>
Commiterjwat

Log Message

Optimize byte[]->String/String->byte[]

Change Summary

Incremental Difference

--- a/jp/bitmeister/asn1/value/HexString.java
+++ b/jp/bitmeister/asn1/value/HexString.java
@@ -29,6 +29,12 @@ import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
2929 public class HexString implements StringItem {
3030
3131 /**
32+ * Hexadecimal digit characters.
33+ */
34+ private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
35+ '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
36+
37+ /**
3238 * The hexadecimal string.
3339 */
3440 private String string;
@@ -49,7 +55,7 @@ public class HexString implements StringItem {
4955 throw ex;
5056 }
5157 StringBuilder builder = new StringBuilder();
52- for (char c: string.toCharArray()) {
58+ for (char c : string.toCharArray()) {
5359 if (Character.digit(c, 16) >= 0) {
5460 builder.append(c);
5561 }
@@ -67,7 +73,8 @@ public class HexString implements StringItem {
6773 public HexString(byte... array) {
6874 StringBuilder builder = new StringBuilder();
6975 for (Byte b : array) {
70- builder.append(String.format("%02X", b));
76+ builder.append(HEX_DIGITS[(b & 0xf0) >> 4]);
77+ builder.append(HEX_DIGITS[b & 0x0f]);
7178 }
7279 string = builder.toString();
7380 }
@@ -89,10 +96,10 @@ public class HexString implements StringItem {
8996 public boolean[] toBinArray() {
9097 boolean[] array = new boolean[string.length() * 4];
9198 int index = 0, mask = 0;
92- byte octet = 0;
99+ int octet = 0;
93100 for (int i = 0; i < array.length; i++) {
94101 if (mask == 0) {
95- octet = Byte.parseByte(string.substring(index++, index), 16);
102+ octet = charToInt(string.charAt(index++));
96103 mask = 8;
97104 }
98105 array[i] = (octet & mask) > 0;
@@ -107,15 +114,14 @@ public class HexString implements StringItem {
107114 * @return An array of {@code byte}.
108115 */
109116 public byte[] toByteArray() {
110- byte[] array = new byte[string.length() / 2 + string.length() % 2];
111- byte octet = 0;
112- for (int i = 0; i < string.length(); i++) {
113- octet = Integer.valueOf(string.substring(i, i + 1), 16).byteValue();
114- if (i % 2 == 0) {
115- array[i / 2] = (byte) (octet << 4);
116- } else {
117- array[i / 2] = (byte) (array[i / 2] + octet);
118- }
117+ byte[] array = new byte[(string.length() + 1) / 2];
118+ int ci = string.length() - 1, bi = array.length - 1;
119+ if (string.length() % 2 != 0) {
120+ array[bi--] = (byte) (charToInt(string.charAt(ci--)) << 4);
121+ }
122+ for (; ci > 0; bi--) {
123+ array[bi] = (byte) charToInt(string.charAt(ci--));
124+ array[bi] += (byte) (charToInt(string.charAt(ci--)) << 4);
119125 }
120126 return array;
121127 }
@@ -130,4 +136,13 @@ public class HexString implements StringItem {
130136 return '\'' + string + "\'H";
131137 }
132138
139+ /**
140+ * Converts a hexadecimal {@code char} to {@code int} value.
141+ *
142+ * @param c A hexadecimal character.
143+ * @return The integer value.
144+ */
145+ private int charToInt(char c) {
146+ return c <= '9' ? c - '0' : c + (10 - 'A');
147+ }
133148 }
Show on old repository browser