Add documentation and remove unused methods

This commit is contained in:
nicksavers 2014-10-11 22:28:40 +02:00
parent ebd13f8e9e
commit 94bf542dfc
2 changed files with 51 additions and 61 deletions

View File

@ -41,16 +41,20 @@ public class ByteUtil {
} }
/** /**
* Omitting sign indication byte * Omitting sign indication byte.
* <br/><br/>
* Instead of {@link org.spongycastle.util.BigIntegers#asUnsignedByteArray(BigInteger)}
* <br/>we use this custom method to avoid an empty array in case of BigInteger.ZERO
* *
* @param b - any big integer number * @param value - any big integer number. A <code>null</code>-value will return <code>null</code>
* @return a byte array representation of this number without a sign byte * @return A byte array without a leading zero byte if present in the signed encoding.
* BigInteger.ZERO will return an array with length 1 and byte-value 0.
*/ */
public static byte[] bigIntegerToBytes(BigInteger b) { public static byte[] bigIntegerToBytes(BigInteger value) {
if (b == null) if (value == null)
return null; return null;
byte[] data = b.toByteArray(); byte[] data = value.toByteArray();
if (data.length != 1 && data[0] == 0) { if (data.length != 1 && data[0] == 0) {
byte[] tmp = new byte[data.length - 1]; byte[] tmp = new byte[data.length - 1];
@ -65,8 +69,8 @@ public class ByteUtil {
* amount will never be larger than smallest input * amount will never be larger than smallest input
* *
* @param a - first input * @param a - first input
* @param b second input * @param b - second input
* @return number of bytes that match * @return Number of bytes that match
*/ */
public static int matchingNibbleLength(byte[] a, byte[] b) { public static int matchingNibbleLength(byte[] a, byte[] b) {
int i = 0; int i = 0;
@ -100,8 +104,7 @@ public class ByteUtil {
* @see {@link Hex#toHexString} * @see {@link Hex#toHexString}
*/ */
public static String toHexString(byte[] data) { public static String toHexString(byte[] data) {
if (data == null) return ""; return data == null ? "" : Hex.toHexString(data);
else return Hex.toHexString(data);
} }
/** /**
@ -177,22 +180,6 @@ public class ByteUtil {
return bytes; return bytes;
} }
/**
* Convert an array of byte arrays to a single string
* <br/>
* Example: "eth" or "eth shh bzz"
*
* @return byte arrays as String
*/
public static String toString(byte[][] arrays) {
StringBuilder sb = new StringBuilder();
for (byte[] array : arrays) {
sb.append(new String(array)).append(" ");
}
if(sb.length() > 0) sb.deleteCharAt(sb.length()-1);
return sb.toString();
}
/** /**
* @param arg - not more that 32 bits * @param arg - not more that 32 bits
* @return - bytes of the value pad with complete to 32 zeroes * @return - bytes of the value pad with complete to 32 zeroes
@ -279,26 +266,16 @@ public class ByteUtil {
return (i >= startIndex || bytes[startIndex] != 0); return (i >= startIndex || bytes[startIndex] != 0);
} }
public static byte[] padAddressWithZeroes(byte[] address){
if (address.length < 20) {
byte[] newAddr = new byte[20];
System.arraycopy(address, 0, newAddr, newAddr.length - address.length, address.length);
return newAddr;
}
return address;
}
/** /**
* Utility function to copy a byte array into a new byte array with given size. * Utility function to copy a byte array into a new byte array with given size.
* If the src length is smaller than the given size, the result will be left-padded * If the src length is smaller than the given size, the result will be left-padded
* with zeros. * with zeros.
* *
* @param src * @param value - a BigInteger with a maximum value of 2^256-1
* @param size
* @return Byte array of given size with a copy of the </code>src</code> * @return Byte array of given size with a copy of the </code>src</code>
*/ */
public static byte[] copyToArray(BigInteger result) { public static byte[] copyToArray(BigInteger value) {
byte[] src = ByteUtil.bigIntegerToBytes(result); byte[] src = ByteUtil.bigIntegerToBytes(value);
byte[] dest = ByteBuffer.allocate(32).array(); byte[] dest = ByteBuffer.allocate(32).array();
System.arraycopy(src, 0, dest, dest.length - src.length, src.length); System.arraycopy(src, 0, dest, dest.length - src.length, src.length);
return dest; return dest;

View File

@ -5,6 +5,8 @@ import static org.junit.Assert.*;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import javassist.bytecode.ByteArray;
import org.junit.Test; import org.junit.Test;
import org.spongycastle.util.BigIntegers; import org.spongycastle.util.BigIntegers;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
@ -18,33 +20,28 @@ public class ByteUtilTest {
assertArrayEquals("test".getBytes(), ByteUtil.appendByte(bytes, b)); assertArrayEquals("test".getBytes(), ByteUtil.appendByte(bytes, b));
} }
// @Test
// public void testBigIntegerToBytes() {
// fail("Not yet implemented");
// }
@Test @Test
public void testToStringDoubleByteArray_1() { public void testBigIntegerToBytes() {
String expected = "eth"; byte[] expecteds = new byte[]{0x0A};
byte[][] input = new byte[][]{"eth".getBytes()}; BigInteger b = BigInteger.valueOf(16772216);
String result = ByteUtil.toString(input); byte[] actuals = ByteUtil.bigIntegerToBytes(b);
assertEquals(expected, result); assertArrayEquals(expecteds, actuals);
} }
@Test @Test
public void testToStringDoubleByteArray_2() { public void testBigIntegerToBytesNegative() {
String expected = "eth shh"; byte[] expecteds = new byte[]{0x01};
byte[][] input = new byte[][]{"eth".getBytes(), "shh".getBytes()}; BigInteger b = BigInteger.valueOf(-16772216);
String result = ByteUtil.toString(input); byte[] actuals = ByteUtil.bigIntegerToBytes(b);
assertEquals(expected, result); assertArrayEquals(expecteds, actuals);
} }
@Test @Test
public void testToStringDoubleByteArray_3() { public void testBigIntegerToBytesZero() {
String expected = ""; byte[] expecteds = new byte[]{0x00};
byte[][] input = new byte[0][]; BigInteger b = BigInteger.ZERO;
String result = ByteUtil.toString(input); byte[] actuals = ByteUtil.bigIntegerToBytes(b);
assertEquals(expected, result); assertArrayEquals(expecteds, actuals);
} }
@Test @Test
@ -63,6 +60,22 @@ public class ByteUtilTest {
public void testByteArrayToInt() { public void testByteArrayToInt() {
assertEquals(0, ByteUtil.byteArrayToInt(null)); assertEquals(0, ByteUtil.byteArrayToInt(null));
assertEquals(0, ByteUtil.byteArrayToInt(new byte[0])); assertEquals(0, ByteUtil.byteArrayToInt(new byte[0]));
// byte[] x = new byte[] { 5,1,7,0,8 };
// long start = System.currentTimeMillis();
// for (int i = 0; i < 100000000; i++) {
// ByteArray.read32bit(x, 0);
// }
// long end = System.currentTimeMillis();
// System.out.println(end - start + "ms");
//
// long start1 = System.currentTimeMillis();
// for (int i = 0; i < 100000000; i++) {
// new BigInteger(1, x).intValue();
// }
// long end1 = System.currentTimeMillis();
// System.out.println(end1 - start1 + "ms");
} }
@Test @Test