RLP { public static byte[] encodeInt(int singleInt) { if ((singleInt & 0xFF) == singleInt) return encodeByte((byte) singleInt); else if ((singleInt & 0xFFFF) == singleInt) return encodeShort((short) singleInt); else if ((singleInt & 0xFFFFFF) == singleInt) return new byte[]{(byte) (OFFSET_SHORT_ITEM + 3), (byte) (singleInt >>> 16), (byte) (singleInt >>> 8), (byte) singleInt}; else { return new byte[]{(byte) (OFFSET_SHORT_ITEM + 4), (byte) (singleInt >>> 24), (byte) (singleInt >>> 16), (byte) (singleInt >>> 8), (byte) singleInt}; } }  static int decodeInt(byte[] data, int index); static long decodeLong(byte[] data, int index); static BigInteger decodeBigInteger(byte[] data, int index); static byte[] decodeIP4Bytes(byte[] data, int index); static int getFirstListElement(byte[] payload, int pos); static int getNextElementIndex(byte[] payload, int pos); static byte getCommandCode(byte[] data); static RLPList decode2(byte[] msgData, int depthLimit); static RLPList decode2(byte[] msgData); static RLPList unwrapList(byte[] msgData); static RLPElement decode2OneItem(byte[] msgData, int startPos); static DecodeResult decode(byte[] data, int pos); static LList decodeLazyList(byte[] data); static LList decodeLazyList(byte[] data, int pos, int length); static byte[] encode(Object input); static byte[] encodeLength(int length, int offset); static byte[] encodeByte(byte singleByte); static byte[] encodeShort(short singleShort); static byte[] encodeInt(int singleInt); static byte[] encodeString(String srcString); static byte[] encodeBigInteger(BigInteger srcBigInteger); static byte[] encodeElement(byte[] srcData); static int calcElementPrefixSize(byte[] srcData); static byte[] encodeListHeader(int size); static byte[] encodeLongElementHeader(int length); static byte[] encodeSet(Set<ByteArrayWrapper> data); static byte[] wrapList(byte[] ... data); static byte[] encodeList(byte[]... elements); static final byte[] EMPTY_ELEMENT_RLP; }
@Test public void testEncodeInt() { byte[] expected = {(byte) 0x80}; byte[] data = encodeInt(0); assertArrayEquals(expected, data); assertEquals(0, RLP.decodeInt(data, 0)); byte[] expected2 = {(byte) 0x78}; data = encodeInt(120); assertArrayEquals(expected2, data); assertEquals(120, RLP.decodeInt(data, 0)); byte[] expected3 = {(byte) 0x7F}; data = encodeInt(127); assertArrayEquals(expected3, data); assertEquals(127, RLP.decodeInt(data, 0)); assertEquals(256, RLP.decodeInt(RLP.encodeInt(256), 0)); assertEquals(255, RLP.decodeInt(RLP.encodeInt(255), 0)); assertEquals(127, RLP.decodeInt(RLP.encodeInt(127), 0)); assertEquals(128, RLP.decodeInt(RLP.encodeInt(128), 0)); data = RLP.encodeInt(1024); assertEquals(1024, RLP.decodeInt(data, 0)); byte[] expected4 = {(byte) 0x82, (byte) 0x76, (byte) 0x5F}; data = encodeInt(30303); assertArrayEquals(expected4, data); assertEquals(30303, RLP.decodeInt(data, 0)); byte[] expected5 = {(byte) 0x82, (byte) 0x4E, (byte) 0xEA}; data = encodeInt(20202); assertArrayEquals(expected5, data); assertEquals(20202, RLP.decodeInt(data, 0)); byte[] expected6 = {(byte) 0x83, 1, 0, 0}; data = encodeInt(65536); assertArrayEquals(expected6, data); assertEquals(65536, RLP.decodeInt(data, 0)); byte[] expected8 = {(byte) 0x84, (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}; data = encodeInt(Integer.MAX_VALUE); assertArrayEquals(expected8, data); assertEquals(Integer.MAX_VALUE, RLP.decodeInt(data, 0)); }
@Ignore @Test(expected = RuntimeException.class) public void cannotEncodeNegativeNumbers() { encodeInt(Integer.MIN_VALUE); }
@Test public void testEncodeInt_7f(){ String result = Hex.toHexString(encodeInt(0x7f)); String expected = "7f"; assertEquals(expected, result); }
@Test public void testEncodeInt_80(){ String result = Hex.toHexString(encodeInt(0x80)); String expected = "8180"; assertEquals(expected, result); }