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 an RLP 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()
    Returns the Keccak hash of the the encoding of this object.
    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 per ByteUtils.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_ARRAY
      RLP-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

      public static RLP bytes​(byte... bytes)
      Creates a new RLP object representing the given byte array.
    • sequence

      public static RLP sequence​(RLP... items)
      Creates a new RLP object representing a sequence of the given sub-items.
    • encoded

      public static RLP encoded​(byte[] encoding)
      Creates a new RLP item holding an already-encoded RLP item (either a sequence or a byte array). Use encode() to access the encoding, and inflate() to retrieve the original layout.
    • sequence

      public static RLP sequence​(Object... items)
      Creates a sequence from the given items, automatically translating them according to their types. Supported types are:
      • RLPLayoutable (including RLP itself)
      • Byte (can pass a byte)
      • Integer (encoded on 4 bytes, can pass an int
      • Long (encoded on 8 bytes, can pass a long
      • byte[]
      • Object[]
    • decode

      public static RLP decode​(byte[] bytes)
      Decodes the given byte sequence to an RLP layout.
      Throws:
      IllegalArgumentException - if the given byte sequence is not well-formed RLP.
    • decode

      public static RLP decode​(String hexString)
      Decodes the given hex string (e.g. 0x123) to an RLP 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

      public RLP[] 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

      public RLP itemAt​(int i)
      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

      public RLP inflate()
      If this object is an encoded RLP item, returns a non-encoded version, otherwise returns this object.
    • hash

      public Hash hash()
      Returns the Keccak hash of the the encoding of this object.
    • stream

      public Stream<RLP> stream()
      Returns a stream over the sequence of sub-items.
      Throws:
      IllegalRLPAccess - if this object does not represent a sequence of sub-items.
    • toHexString

      public String toHexString()
      The hex-string representation of the binary encoding of this RLP item, including leading 0 if any, as per ByteUtils.toFullHexString(byte[]).
    • rlpLayout

      public RLP rlpLayout()
      Description copied from interface: RLPLayoutable
      Returns the RLP layout for the object.
      Specified by:
      rlpLayout in interface RLPLayoutable
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object