RLP { public static byte[] encodeElement(byte[] srcData) { if (isNullOrZeroArray(srcData)) { return new byte[]{(byte) OFFSET_SHORT_ITEM}; } else if (isSingleZero(srcData)) { return srcData; } else if (srcData.length == 1 && (srcData[0] & 0xFF) < 0x80) { return srcData; } else if (srcData.length < SIZE_THRESHOLD) { byte length = (byte) (OFFSET_SHORT_ITEM + srcData.length); byte[] data = Arrays.copyOf(srcData, srcData.length + 1); System.arraycopy(data, 0, data, 1, srcData.length); data[0] = length; return data; } else { int tmpLength = srcData.length; byte lengthOfLength = 0; while (tmpLength != 0) { ++lengthOfLength; tmpLength = tmpLength >> 8; } byte[] data = new byte[1 + lengthOfLength + srcData.length]; data[0] = (byte) (OFFSET_LONG_ITEM + lengthOfLength); tmpLength = srcData.length; for (int i = lengthOfLength; i > 0; --i) { data[i] = (byte) (tmpLength & 0xFF); tmpLength = tmpLength >> 8; } System.arraycopy(srcData, 0, data, 1 + lengthOfLength, srcData.length); return data; } }  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 testEncodeElementNull() { byte[] actuals = encodeElement(null); assertArrayEquals(new byte[]{(byte) 0x80}, actuals); }
@Test public void testEncodeElementZero() { byte[] actuals = encodeElement(new byte[]{0x00}); assertArrayEquals(new byte[]{0x00}, actuals); }
@Test public void testEncodeElementOne() { byte[] actuals = encodeElement(new byte[]{0x01}); assertArrayEquals(new byte[]{(byte) 0x01}, actuals); }