RLP { public static DecodeResult decode(byte[] data, int pos) { if (data == null || data.length < 1) { return null; } int prefix = data[pos] & 0xFF; if (prefix == OFFSET_SHORT_ITEM) { return new DecodeResult(pos + 1, ""); } else if (prefix < OFFSET_SHORT_ITEM) { return new DecodeResult(pos + 1, new byte[]{data[pos]}); } else if (prefix <= OFFSET_LONG_ITEM) { int len = prefix - OFFSET_SHORT_ITEM; return new DecodeResult(pos + 1 + len, copyOfRange(data, pos + 1, pos + 1 + len)); } else if (prefix < OFFSET_SHORT_LIST) { int lenlen = prefix - OFFSET_LONG_ITEM; int lenbytes = byteArrayToInt(copyOfRange(data, pos + 1, pos + 1 + lenlen)); verifyLength(lenbytes, data.length - pos - 1 - lenlen); return new DecodeResult(pos + 1 + lenlen + lenbytes, copyOfRange(data, pos + 1 + lenlen, pos + 1 + lenlen + lenbytes)); } else if (prefix <= OFFSET_LONG_LIST) { int len = prefix - OFFSET_SHORT_LIST; int prevPos = pos; pos++; return decodeList(data, pos, prevPos, len); } else if (prefix <= 0xFF) { int lenlen = prefix - OFFSET_LONG_LIST; int lenlist = byteArrayToInt(copyOfRange(data, pos + 1, pos + 1 + lenlen)); pos = pos + lenlen + 1; int prevPos = lenlist; return decodeList(data, pos, prevPos, lenlist); } else { throw new RuntimeException("Only byte values between 0x00 and 0xFF are supported, but got: " + prefix); } }  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 testRlpDecode() { int pos = 0; String emptyString; byte[] decodedData; Object[] decodedList; emptyString = (String) decode(Hex.decode(result01), pos).getDecoded(); assertEquals("", emptyString); emptyString = (String) decode(Hex.decode(result02), pos).getDecoded(); assertEquals(test02, emptyString); decodedData = (byte[]) decode(Hex.decode(result03), pos).getDecoded(); assertEquals(test03, bytesToAscii(decodedData)); decodedData = (byte[]) decode(Hex.decode(result04), pos).getDecoded(); assertEquals(test04, bytesToAscii(decodedData)); decodedData = (byte[]) decode(Hex.decode(result05), pos).getDecoded(); assertEquals(test05, bytesToAscii(decodedData)); decodedList = (Object[]) decode(Hex.decode(result06), pos).getDecoded(); assertEquals(test06[0], bytesToAscii((byte[]) decodedList[0])); assertEquals(test06[1], bytesToAscii((byte[]) decodedList[1])); decodedList = (Object[]) decode(Hex.decode(result07), pos).getDecoded(); assertEquals(test07[0], bytesToAscii((byte[]) decodedList[0])); assertEquals(test07[1], bytesToAscii((byte[]) decodedList[1])); assertEquals(test07[2], bytesToAscii((byte[]) decodedList[2])); decodedData = (byte[]) decode(Hex.decode(result08), pos).getDecoded(); assertEquals(test08, byteArrayToInt(decodedData)); decodedData = (byte[]) decode(Hex.decode(result09), pos).getDecoded(); assertEquals(test09, byteArrayToInt(decodedData)); decodedData = (byte[]) decode(Hex.decode(result10), pos).getDecoded(); assertEquals(test10, byteArrayToInt(decodedData)); decodedData = (byte[]) decode(Hex.decode(result11), pos).getDecoded(); assertEquals(test11, byteArrayToInt(decodedData)); decodedData = (byte[]) decode(Hex.decode(result12), pos).getDecoded(); assertTrue(test12.compareTo(new BigInteger(1, decodedData)) == 0); decodedData = (byte[]) decode(Hex.decode(result13), pos).getDecoded(); assertTrue(test13.compareTo(new BigInteger(1, decodedData)) == 0); Object testObject1 = decode(Hex.decode(result14), pos).getDecoded(); assertTrue(DeepEquals.deepEquals(expected14, testObject1)); Object testObject2 = decode(Hex.decode(result15), pos).getDecoded(); assertTrue(DeepEquals.deepEquals(test15, testObject2)); Object testObject3 = decode(Hex.decode(result16), pos).getDecoded(); assertTrue(DeepEquals.deepEquals(expected16, testObject3)); }