diff --git a/src/main/java/com/esaulpaugh/headlong/abi/Function.java b/src/main/java/com/esaulpaugh/headlong/abi/Function.java index a2331725c..d5b670103 100644 --- a/src/main/java/com/esaulpaugh/headlong/abi/Function.java +++ b/src/main/java/com/esaulpaugh/headlong/abi/Function.java @@ -254,7 +254,7 @@ public Tuple decodeCall(byte[] array) { } public Tuple decodeCall(ByteBuffer abiBuffer) { - byte[] unitBuffer = ABIType.newUnitBuffer(); + final byte[] unitBuffer = ABIType.newUnitBuffer(); abiBuffer.get(unitBuffer, 0, SELECTOR_LEN); final byte[] selector = this.selector; for(int i = 0; i < SELECTOR_LEN; i++) { diff --git a/src/main/java/com/esaulpaugh/headlong/abi/util/BizarroIntegers.java b/src/main/java/com/esaulpaugh/headlong/abi/util/BizarroIntegers.java index 2457a229d..c48d66e81 100644 --- a/src/main/java/com/esaulpaugh/headlong/abi/util/BizarroIntegers.java +++ b/src/main/java/com/esaulpaugh/headlong/abi/util/BizarroIntegers.java @@ -67,7 +67,7 @@ public static int putByte(byte val, byte[] o, int i) { } public static int putShort(short val, byte[] o, int i) { - byte b = 0; + byte b; int v = val; final int n; if (v != -1) { @@ -75,12 +75,12 @@ public static int putShort(short val, byte[] o, int i) { if ((v >>= Byte.SIZE) != -1) { n = 2; } else n = 1; - } else n = 0; + } else return 0; return Integers.insertBytes(n, o, i, (byte) 0, (byte) 0, (byte) v, b); } public static int putInt(int val, byte[] o, int i) { - byte b = 0, c = 0, d = 0; + byte b = 0, c = 0, d; final int n; if (val != -1) { d = (byte) val; @@ -93,12 +93,12 @@ public static int putInt(int val, byte[] o, int i) { } else n = 3; } else n = 2; } else n = 1; - } else n = 0; + } else return 0; return Integers.insertBytes(n, o, i, (byte) val, b, c, d); } public static int putLong(long val, byte[] o, int i) { - byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0; + byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h; final int n; if (val != -1) { h = (byte) val; @@ -123,12 +123,12 @@ public static int putLong(long val, byte[] o, int i) { } else n = 3; } else n = 2; } else n = 1; - } else n = 0; + } else return 0; return Integers.insertBytes(n, o, i, (byte) val, b, c, d, e, f, g, h); } public static int putLong(long val, ByteBuffer o) { - byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0; + byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h; final int n; if (val != -1) { h = (byte) val; @@ -153,27 +153,12 @@ public static int putLong(long val, ByteBuffer o) { } else n = 3; } else n = 2; } else n = 1; - } else n = 0; + } else return 0; return Integers.insertBytes(n, o, (byte) val, b, c, d, e, f, g, h); } // ******************* - private static byte _getByte(byte[] buffer, int i, int len) { - switch (len) { - case 0: return 0; - case 1: return buffer[i]; - default: throw new IllegalArgumentException("len is out of range: " + len); - } - } - - private static short _getShort(byte[] buffer, int i, int len) { - int shiftAmount = 0; - int val = 0; - switch (len) { /* cases 2 through 1 fall through */ - case 2: val = buffer[i+1] & 0xFF; shiftAmount = Byte.SIZE; // & 0xFF to promote to int before left shift - case 1: val |= (buffer[i] & 0xFF) << shiftAmount; - case 0: return (short) val; - default: throw new IllegalArgumentException("len is out of range: " + len); - } + private static int _getShortInt(byte[] buffer, int i) { + return (buffer[i+1] & 0xFF) | ((buffer[i] & 0xFF) << Byte.SIZE); } private static int _getInt(byte[] buffer, int i, int len) { @@ -209,7 +194,7 @@ private static long _getLong(final byte[] buffer, final int i, final int len) { public static byte getByte(byte[] buffer, int index, int len) { switch (len) { case 0: return (byte) 0xFF; - case 1: return _getByte(buffer, index, 1); + case 1: return buffer[index]; default: throw new IllegalArgumentException("len is out of range: " + len); } } @@ -218,8 +203,8 @@ public static short getShort(byte[] buffer, int index, int len) { // do sign extension for negative shorts, i.e. len < 2 switch (len) { case 0: return (short) 0xFFFF; - case 1: return (short) (0xFFFFFF00 | _getByte(buffer, index, 1)); - case 2: return _getShort(buffer, index, 2); + case 1: return (short) (0xFFFFFF00 | buffer[index]); + case 2: return (short) _getShortInt(buffer, index); default: throw new IllegalArgumentException("len is out of range: " + len); } } @@ -228,8 +213,8 @@ public static int getInt(byte[] buffer, int index, int len) { // do sign extension for negative ints, i.e. len < 4 switch (len) { case 0: return 0xFFFFFFFF; - case 1: return 0xFFFFFF00 | _getByte(buffer, index, 1); - case 2: return 0xFFFF0000 | _getShort(buffer, index, 2); + case 1: return 0xFFFFFF00 | buffer[index]; + case 2: return 0xFFFF0000 | _getShortInt(buffer, index); case 3: return 0xFF000000 | _getInt(buffer, index, 3); case 4: return _getInt(buffer, index, 4); default: throw new IllegalArgumentException("len is out of range: " + len); @@ -240,8 +225,8 @@ public static long getLong(final byte[] buffer, final int index, final int len) // do sign extension for negative longs, i.e. len < 8 switch (len) { case 0: return 0xFFFFFFFF_FFFFFFFFL; - case 1: return 0xFFFFFFFF_FFFFFF00L | _getByte(buffer, index, 1); - case 2: return 0xFFFFFFFF_FFFF0000L | _getShort(buffer, index, 2); + case 1: return 0xFFFFFFFF_FFFFFF00L | buffer[index]; + case 2: return 0xFFFFFFFF_FFFF0000L | _getShortInt(buffer, index); case 3: return 0xFFFFFFFF_FF000000L | _getInt(buffer, index, 3); case 4: return 0xFFFFFFFF_00000000L | _getInt(buffer, index, 4); case 5: return 0xFFFFFF00_00000000L | _getLong(buffer, index, 5); diff --git a/src/main/java/com/esaulpaugh/headlong/rlp/eip778/KeyValuePair.java b/src/main/java/com/esaulpaugh/headlong/rlp/eip778/KeyValuePair.java index 9f1ce3647..ee47b8ff8 100644 --- a/src/main/java/com/esaulpaugh/headlong/rlp/eip778/KeyValuePair.java +++ b/src/main/java/com/esaulpaugh/headlong/rlp/eip778/KeyValuePair.java @@ -20,8 +20,6 @@ import java.util.Arrays; import static com.esaulpaugh.headlong.util.Strings.UTF_8; -import static com.esaulpaugh.headlong.util.Strings.decode; -import static com.esaulpaugh.headlong.util.Strings.encode; public final class KeyValuePair implements Comparable { @@ -40,8 +38,8 @@ public final class KeyValuePair implements Comparable { private final byte[] value; public KeyValuePair(String key, String value, int valueEncoding) { - this.key = decode(key, UTF_8); - this.value = decode(value, valueEncoding); + this.key = Strings.decode(key, UTF_8); + this.value = Strings.decode(value, valueEncoding); } public KeyValuePair(byte[] key, byte[] value) { @@ -71,16 +69,16 @@ public boolean equals(Object o) { @Override public String toString() { - return encode(key, UTF_8) + " --> " + encode(value, Strings.HEX); + return Strings.encode(key, UTF_8) + " --> " + Strings.encode(value, Strings.HEX); } @Override public int compareTo(KeyValuePair o) { int result = compare(key, o.key); - if (result == 0) { - throw new IllegalArgumentException("duplicate key: " + encode(o.key, UTF_8)); + if (result != 0) { + return result; } - return result; + throw new IllegalArgumentException("duplicate key: " + Strings.encode(o.key, UTF_8)); } private static int compare(byte[] a, byte[] b) { diff --git a/src/main/java/com/esaulpaugh/headlong/rlp/util/Integers.java b/src/main/java/com/esaulpaugh/headlong/rlp/util/Integers.java index 1828af9fa..8e8bf4bb1 100644 --- a/src/main/java/com/esaulpaugh/headlong/rlp/util/Integers.java +++ b/src/main/java/com/esaulpaugh/headlong/rlp/util/Integers.java @@ -113,7 +113,7 @@ public static int putByte(byte val, byte[] o, int i) { * @return the number of bytes inserted */ public static int putShort(short val, byte[] o, int i) { - byte b = 0; + byte b; final int n; if(val != 0) { b = (byte) val; @@ -122,7 +122,7 @@ public static int putShort(short val, byte[] o, int i) { if (val != 0) { n = 2; } else n = 1; - } else n = 0; + } else return 0; return insertBytes(n, o, i, (byte) 0, (byte) 0, (byte) val, b); } @@ -138,7 +138,7 @@ public static int putShort(short val, byte[] o, int i) { * @return the number of bytes inserted */ public static int putInt(int val, byte[] o, int i) { - byte b = 0, c = 0, d = 0; + byte b = 0, c = 0, d; final int n; if(val != 0) { d = (byte) val; @@ -151,7 +151,7 @@ public static int putInt(int val, byte[] o, int i) { } else n = 3; } else n = 2; } else n = 1; - } else n = 0; + } else return 0; return insertBytes(n, o, i, (byte) val, b, c, d); } @@ -167,7 +167,7 @@ public static int putInt(int val, byte[] o, int i) { * @return the number of bytes inserted */ public static int putLong(long val, byte[] o, int i) { - byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0; + byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h; final int n; if(val != 0) { h = (byte) val; @@ -192,12 +192,12 @@ public static int putLong(long val, byte[] o, int i) { } else n = 3; } else n = 2; } else n = 1; - } else n = 0; + } else return 0; return insertBytes(n, o, i, (byte) val, b, c, d, e, f, g, h); } public static int putLong(long val, ByteBuffer o) { - byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0; + byte b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h; final int n; if(val != 0) { h = (byte) val; @@ -222,7 +222,7 @@ public static int putLong(long val, ByteBuffer o) { } else n = 3; } else n = 2; } else n = 1; - } else n = 0; + } else return 0; return insertBytes(n, o, (byte) val, b, c, d, e, f, g, h); } diff --git a/src/test/java/com/esaulpaugh/headlong/abi/util/BizarroIntegersTest.java b/src/test/java/com/esaulpaugh/headlong/abi/util/BizarroIntegersTest.java index eb7abe7c2..c870e2e9c 100644 --- a/src/test/java/com/esaulpaugh/headlong/abi/util/BizarroIntegersTest.java +++ b/src/test/java/com/esaulpaugh/headlong/abi/util/BizarroIntegersTest.java @@ -108,7 +108,7 @@ private static void testLongs(int iterations, Supplier supplier, byte[] ei int n = BizarroIntegers.putLong(lo, eight, 0); long r = BizarroIntegers.getLong(eight, 0, n); if(lo != r) { - throw new AssertionError(lo + "!= " + r); + throw new AssertionError(lo + " != " + r); } } }