Package com.norswap.nanoeth.rlp
Class RLP
java.lang.Object
com.norswap.nanoeth.rlp.RLP
- All Implemented Interfaces:
RLPLayoutable
public final class RLP extends Object implements RLPLayoutable
An object that can be
encoded
in RLP format, or decoded
from a byte array in RLP format.
This represents either a sequence of sub-items, a byte array, or a binary-encoded RLP item (either a sequence or byte array).
We allow representing already-encoded items in order to enable incremental RLP encoding (see the README of this package for more information).
-
Field Summary
Fields Modifier and Type Field Description static byte[]
RLP_EMPTY_BYTE_ARRAY
RLP-encoding of an empty RLP byte array, which is sometimes used to represent absent values. -
Method Summary
Modifier and Type Method Description byte
byteAt(int i)
Returns the byte at the given index.byte[]
bytes()
Returns the byte array that this object represents.static RLP
bytes(byte... bytes)
Creates a new RLP object representing the given byte array.static RLP
decode(byte[] bytes)
Decodes the given byte sequence to anRLP
layout.static RLP
decode(String hexString)
Decodes the given hex string (e.g.byte[]
encode()
Returns the binary RLP encoding of this object.static RLP
encoded(byte[] encoding)
Creates a new RLP item holding an already-encoded RLP item (either a sequence or a byte array).static boolean
encodesBytes(byte[] encoding)
Does the given RLP encoding encode a byte array?static boolean
encodesSequence(byte[] encoding)
Does the given RLP encoding encode an item sequence?boolean
equals(Object o)
Hash
hash()
int
hashCode()
RLP
inflate()
If this object is an encoded RLP item, returns a non-encoded version, otherwise returns this object.boolean
isBytes()
True iff this object represents a byte array.boolean
isEncoded()
True iff this object represents an already-encoded RLP item.boolean
isSequence()
True iff this object represents a sequence of sub-items.RLP
itemAt(int i)
Returns the sub-item at the given index.RLP[]
items()
Returns an array containing the sub-items that this object represents.RLP
rlpLayout()
Returns the RLP layout for the object.static RLP
sequence(RLP... items)
Creates a new RLP object representing a sequence of the given sub-items.static RLP
sequence(Object... items)
Creates a sequence from the given items, automatically translating them according to their types.Stream<RLP>
stream()
Returns a stream over the sequence of sub-items.String
toHexString()
The hex-string representation of the binary encoding of this RLP item, including leading 0 if any, as perByteUtils.toFullHexString(byte[])
.String
toString()
static byte[]
unwrap(byte[] encoding)
If the given encoding encodes a byte array, returns that byte array, otherwise return the encoding.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.norswap.nanoeth.rlp.RLPLayoutable
rlpEncode, rlpHexString
-
Field Details
-
RLP_EMPTY_BYTE_ARRAY
public static final byte[] RLP_EMPTY_BYTE_ARRAYRLP-encoding of an empty RLP byte array, which is sometimes used to represent absent values.
-
-
Method Details
-
encodesBytes
public static boolean encodesBytes(byte[] encoding)Does the given RLP encoding encode a byte array? -
encodesSequence
public static boolean encodesSequence(byte[] encoding)Does the given RLP encoding encode an item sequence? -
unwrap
public static byte[] unwrap(byte[] encoding)If the given encoding encodes a byte array, returns that byte array, otherwise return the encoding. -
bytes
Creates a new RLP object representing the given byte array. -
sequence
Creates a new RLP object representing a sequence of the given sub-items. -
encoded
-
sequence
Creates a sequence from the given items, automatically translating them according to their types. Supported types are:RLPLayoutable
(includingRLP
itself)Byte
(can pass abyte
)Integer
(encoded on 4 bytes, can pass anint
Long
(encoded on 8 bytes, can pass along
byte[]
Object[]
-
decode
Decodes the given byte sequence to anRLP
layout.- Throws:
IllegalArgumentException
- if the given byte sequence is not well-formed RLP.
-
decode
Decodes the given hex string (e.g. 0x123) to anRLP
layout.- Throws:
IllegalArgumentException
- if the given hex string is not well-formed RLP.
-
isBytes
public boolean isBytes()True iff this object represents a byte array. -
isSequence
public boolean isSequence()True iff this object represents a sequence of sub-items. -
isEncoded
public boolean isEncoded()True iff this object represents an already-encoded RLP item. -
bytes
public byte[] bytes()Returns the byte array that this object represents.- Throws:
IllegalRLPAccess
- if this object does not represent a byte array.
-
items
Returns an array containing the sub-items that this object represents.- Throws:
IllegalRLPAccess
- if this object does not represent a sequence of sub-items.
-
byteAt
public byte byteAt(int i)Returns the byte at the given index.- Throws:
IllegalRLPAccess
- if this object does not represent a byte array, or if the index is out of bounds.
-
itemAt
Returns the sub-item at the given index.- Throws:
IllegalRLPAccess
- if this object does not represent a sequence of sub-items, or the index is out of bounds.
-
encode
public byte[] encode()Returns the binary RLP encoding of this object. -
inflate
If this object is an encoded RLP item, returns a non-encoded version, otherwise returns this object. -
hash
-
stream
Returns a stream over the sequence of sub-items.- Throws:
IllegalRLPAccess
- if this object does not represent a sequence of sub-items.
-
toHexString
The hex-string representation of the binary encoding of this RLP item, including leading 0 if any, as perByteUtils.toFullHexString(byte[])
. -
rlpLayout
Description copied from interface:RLPLayoutable
Returns the RLP layout for the object.- Specified by:
rlpLayout
in interfaceRLPLayoutable
-
equals
-
hashCode
public int hashCode() -
toString
-