RLP { public static byte[] encodeLength(int length, int offset) { if (length < SIZE_THRESHOLD) { byte firstByte = (byte) (length + offset); return new byte[] { firstByte }; } else if (length < MAX_ITEM_LENGTH) { byte[] binaryLength; if (length > 0xFF) binaryLength = BigInteger.valueOf(length).toByteArray(); else binaryLength = new byte[] { (byte) length }; byte firstByte = (byte) (binaryLength.length + offset + SIZE_THRESHOLD - 1); return concatenate(new byte[] { firstByte }, binaryLength); } else { throw new RuntimeException("Input too long"); } }  static int decodeInt(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 void fullTraverse(byte[] msgData, int level, int startPos,
			int endPos, int levelToIndex, Queue<Integer> index); static byte getCommandCode(byte[] data); static RLPList decode2(byte[] msgData); static DecodeResult decode(byte[] data, int pos); 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 byte[] encodeList(byte[]... elements);  }
@Test public void testEncodeLength() { int length = 1; int offset = 128; byte[] encodedLength = RLP.encodeLength(length, offset); String expected = "81"; assertEquals(expected, Hex.toHexString(encodedLength)); length = 56; offset = 192; encodedLength = RLP.encodeLength(length, offset); expected = "f838"; assertEquals(expected, Hex.toHexString(encodedLength)); }
@Test @Ignore public void unsupportedLength() { int length = 56; int offset = 192; byte[] encodedLength; double maxLength = Math.pow(256, 8); try { encodedLength = RLP.encodeLength( (int) maxLength, offset); System.out.println("length: " + length + ", offset: " + offset + ", encoded: " + Arrays.toString(encodedLength)); fail("Expecting RuntimeException: 'Input too long'"); } catch(RuntimeException e) { } }