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
* @return a byte array representation of this number without a sign byte
* @param value - any big integer number. A <code>null</code>-value will return <code>null</code>
* @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) {
if (b == null)
public static byte[] bigIntegerToBytes(BigInteger value) {
if (value == null)
return null;
byte[] data = b.toByteArray();
byte[] data = value.toByteArray();
if (data.length != 1 && data[0] == 0) {
byte[] tmp = new byte[data.length - 1];
@ -65,8 +69,8 @@ public class ByteUtil {
* amount will never be larger than smallest input
*
* @param a - first input
* @param b second input
* @return number of bytes that match
* @param b - second input
* @return Number of bytes that match
*/
public static int matchingNibbleLength(byte[] a, byte[] b) {
int i = 0;
@ -100,8 +104,7 @@ public class ByteUtil {
* @see {@link Hex#toHexString}
*/
public static String toHexString(byte[] data) {
if (data == null) return "";
else return Hex.toHexString(data);
return data == null ? "" : Hex.toHexString(data);
}
/**
@ -177,22 +180,6 @@ public class ByteUtil {
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
* @return - bytes of the value pad with complete to 32 zeroes
@ -279,26 +266,16 @@ public class ByteUtil {
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.
* If the src length is smaller than the given size, the result will be left-padded
* with zeros.
*
* @param src
* @param size
* @param value - a BigInteger with a maximum value of 2^256-1
* @return Byte array of given size with a copy of the </code>src</code>
*/
public static byte[] copyToArray(BigInteger result) {
byte[] src = ByteUtil.bigIntegerToBytes(result);
public static byte[] copyToArray(BigInteger value) {
byte[] src = ByteUtil.bigIntegerToBytes(value);
byte[] dest = ByteBuffer.allocate(32).array();
System.arraycopy(src, 0, dest, dest.length - src.length, src.length);
return dest;

View File

@ -5,6 +5,8 @@ import static org.junit.Assert.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import javassist.bytecode.ByteArray;
import org.junit.Test;
import org.spongycastle.util.BigIntegers;
import org.spongycastle.util.encoders.Hex;
@ -18,33 +20,28 @@ public class ByteUtilTest {
assertArrayEquals("test".getBytes(), ByteUtil.appendByte(bytes, b));
}
// @Test
// public void testBigIntegerToBytes() {
// fail("Not yet implemented");
// }
@Test
public void testToStringDoubleByteArray_1() {
String expected = "eth";
byte[][] input = new byte[][]{"eth".getBytes()};
String result = ByteUtil.toString(input);
assertEquals(expected, result);
public void testBigIntegerToBytes() {
byte[] expecteds = new byte[]{0x0A};
BigInteger b = BigInteger.valueOf(16772216);
byte[] actuals = ByteUtil.bigIntegerToBytes(b);
assertArrayEquals(expecteds, actuals);
}
@Test
public void testToStringDoubleByteArray_2() {
String expected = "eth shh";
byte[][] input = new byte[][]{"eth".getBytes(), "shh".getBytes()};
String result = ByteUtil.toString(input);
assertEquals(expected, result);
public void testBigIntegerToBytesNegative() {
byte[] expecteds = new byte[]{0x01};
BigInteger b = BigInteger.valueOf(-16772216);
byte[] actuals = ByteUtil.bigIntegerToBytes(b);
assertArrayEquals(expecteds, actuals);
}
@Test
public void testToStringDoubleByteArray_3() {
String expected = "";
byte[][] input = new byte[0][];
String result = ByteUtil.toString(input);
assertEquals(expected, result);
@Test
public void testBigIntegerToBytesZero() {
byte[] expecteds = new byte[]{0x00};
BigInteger b = BigInteger.ZERO;
byte[] actuals = ByteUtil.bigIntegerToBytes(b);
assertArrayEquals(expecteds, actuals);
}
@Test
@ -63,6 +60,22 @@ public class ByteUtilTest {
public void testByteArrayToInt() {
assertEquals(0, ByteUtil.byteArrayToInt(null));
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