Class DelegatingMutableBytes

java.lang.Object
org.apache.tuweni.bytes.DelegatingMutableBytes
All Implemented Interfaces:
Comparable<Bytes>, Bytes, MutableBytes

public class DelegatingMutableBytes extends Object implements MutableBytes
A class that holds and delegates all operations to its inner bytes field.

This class may be used to create more types that represent bytes, but need a different name for business logic.

  • Field Summary

    Fields inherited from interface org.apache.tuweni.bytes.MutableBytes

    EMPTY
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
     
  • Method Summary

    Modifier and Type
    Method
    Description
    and(Bytes other)
    Return a bit-wise AND of these bytes and the supplied bytes.
    <T extends MutableBytes>
    T
    and(Bytes other, T result)
    Calculate a bit-wise AND of these bytes and the supplied bytes.
    void
    appendTo(io.vertx.core.buffer.Buffer buffer)
    Append the bytes of this value to the provided Vert.x Buffer.
    void
    appendTo(ByteBuffer byteBuffer)
    Append the bytes of this value to the ByteBuffer.
    int
    Provides the number of bits following and including the highest-order ("leftmost") one-bit, or zero if all bits are zero.
    void
    Set all bytes in this value to 0.
    Return a slice over the common prefix between this set of bytes and another.
    int
    Return the number of bytes in common between this set of bytes and another.
    Return a value equivalent to this one but guaranteed to 1) be deeply immutable (i.e.
    void
    copyTo(MutableBytes destination)
    Copy the bytes of this value to the provided mutable one, which must have the same size.
    void
    copyTo(MutableBytes destination, int destinationOffset)
    Copy the bytes of this value to the provided mutable one from a particular offset.
    Decrements the value of the bytes by 1, treating the value as big endian.
    boolean
     
    void
    fill(byte b)
    Fill all the bytes of this value with the specified byte.
    byte
    get(int i)
    Retrieve a byte in this value.
    int
    getInt(int i)
    Retrieve the 4 bytes starting at the provided index in this value as an integer.
    long
    getLong(int i)
    Retrieves the 8 bytes starting at the provided index in this value as a long.
    int
     
    boolean
    Whether the bytes start with a zero bit value.
    boolean
    Whether the bytes start with a zero byte value.
    Increments the value of the bytes by 1, treating the value as big endian.
    boolean
    Whether this value has only zero bytes.
    Return a new mutable value initialized with the content of this value.
    mutableSlice(int i, int length)
    Create a mutable slice of the bytes of this value.
    <T extends MutableBytes>
    T
    not(T result)
    Calculate a bit-wise NOT of these bytes.
    int
    Provides the number of leading zero bytes of the value
    int
    Provides the number of zero bits preceding the highest-order ("leftmost") one-bit, or size() * 8 if all bits * are zero.
    or(Bytes other)
    Return a bit-wise OR of these bytes and the supplied bytes.
    <T extends MutableBytes>
    T
    or(Bytes other, T result)
    Calculate a bit-wise OR of these bytes and the supplied bytes.
    void
    set(int i, byte b)
    Set a byte in this value.
    void
    set(int i, Bytes b)
    Set a byte in this value.
    void
    setInt(int i, int value)
    Set the 4 bytes starting at the specified index to the specified integer value.
    void
    setLong(int i, long value)
    Set the 8 bytes starting at the specified index to the specified long value.
    <T extends MutableBytes>
    T
    shiftLeft(int distance, T result)
    Shift all bits in this value to the left.
    <T extends MutableBytes>
    T
    shiftRight(int distance, T result)
    Shift all bits in this value to the right.
    int
    Provides the number of bytes this value represents.
    slice(int index)
    Create a new value representing (a view of) a slice of the bytes of this value.
    slice(int index, int length)
    Create a new value representing (a view of) a slice of the bytes of this value.
    byte[]
    Extract the bytes of this value into a byte array.
    byte[]
    Get the bytes represented by this value as byte array.
    The BigInteger corresponding to interpreting these bytes as a two's-complement signed integer.
    Provides this value represented as hexadecimal, starting with "0x".
    int
    The value corresponding to interpreting these bytes as an integer.
    long
    The value corresponding to interpreting these bytes as a long.
    Provides this value represented as a minimal hexadecimal string (without any leading zero)
    Return the hexadecimal string representation of this value.
    The BigInteger corresponding to interpreting these bytes as an unsigned integer.
    void
    Update the provided message digest with the bytes of this value.
    xor(Bytes other)
    Return a bit-wise XOR of these bytes and the supplied bytes.
    <T extends MutableBytes>
    T
    xor(Bytes other, T result)
    Calculate a bit-wise XOR of these bytes and the supplied bytes.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • DelegatingMutableBytes

      protected DelegatingMutableBytes(MutableBytes delegate)
  • Method Details

    • set

      public void set(int i, byte b)
      Description copied from interface: MutableBytes
      Set a byte in this value.
      Specified by:
      set in interface MutableBytes
      Parameters:
      i - The index of the byte to set.
      b - The value to set that byte to.
    • set

      public void set(int i, Bytes b)
      Description copied from interface: MutableBytes
      Set a byte in this value.
      Specified by:
      set in interface MutableBytes
      Parameters:
      i - The offset of the bytes to set.
      b - The value to set bytes to.
    • setInt

      public void setInt(int i, int value)
      Description copied from interface: MutableBytes
      Set the 4 bytes starting at the specified index to the specified integer value.
      Specified by:
      setInt in interface MutableBytes
      Parameters:
      i - The index, which must less than or equal to size() - 4.
      value - The integer value.
    • setLong

      public void setLong(int i, long value)
      Description copied from interface: MutableBytes
      Set the 8 bytes starting at the specified index to the specified long value.
      Specified by:
      setLong in interface MutableBytes
      Parameters:
      i - The index, which must less than or equal to size() - 8.
      value - The long value.
    • increment

      public MutableBytes increment()
      Description copied from interface: MutableBytes
      Increments the value of the bytes by 1, treating the value as big endian.

      If incrementing overflows the value then all bits flip, i.e. incrementing 0xFFFF will return 0x0000.

      Specified by:
      increment in interface MutableBytes
      Returns:
      this value
    • decrement

      public MutableBytes decrement()
      Description copied from interface: MutableBytes
      Decrements the value of the bytes by 1, treating the value as big endian.

      If decrementing underflows the value then all bits flip, i.e. decrementing 0x0000 will return 0xFFFF.

      Specified by:
      decrement in interface MutableBytes
      Returns:
      this value
    • mutableSlice

      public MutableBytes mutableSlice(int i, int length)
      Description copied from interface: MutableBytes
      Create a mutable slice of the bytes of this value.

      Note: the resulting slice is only a view over the original value. Holding a reference to the returned slice may hold more memory than the slide represents. Use Bytes.copy() on the returned slice to avoid this.

      Specified by:
      mutableSlice in interface MutableBytes
      Parameters:
      i - The start index for the slice.
      length - The length of the resulting value.
      Returns:
      A new mutable view over the bytes of this value from index i (included) to index i + length (excluded).
    • fill

      public void fill(byte b)
      Description copied from interface: MutableBytes
      Fill all the bytes of this value with the specified byte.
      Specified by:
      fill in interface MutableBytes
      Parameters:
      b - The byte to use to fill the value.
    • clear

      public void clear()
      Description copied from interface: MutableBytes
      Set all bytes in this value to 0.
      Specified by:
      clear in interface MutableBytes
    • size

      public int size()
      Description copied from interface: Bytes
      Provides the number of bytes this value represents.
      Specified by:
      size in interface Bytes
      Returns:
      The number of bytes this value represents.
    • get

      public byte get(int i)
      Description copied from interface: Bytes
      Retrieve a byte in this value.
      Specified by:
      get in interface Bytes
      Parameters:
      i - The index of the byte to fetch within the value (0-indexed).
      Returns:
      The byte at index i in this value.
    • getInt

      public int getInt(int i)
      Description copied from interface: Bytes
      Retrieve the 4 bytes starting at the provided index in this value as an integer.
      Specified by:
      getInt in interface Bytes
      Parameters:
      i - The index from which to get the int, which must less than or equal to size() - 4.
      Returns:
      An integer whose value is the 4 bytes from this value starting at index i.
    • toInt

      public int toInt()
      Description copied from interface: Bytes
      The value corresponding to interpreting these bytes as an integer.
      Specified by:
      toInt in interface Bytes
      Returns:
      An value corresponding to this value interpreted as an integer.
    • getLong

      public long getLong(int i)
      Description copied from interface: Bytes
      Retrieves the 8 bytes starting at the provided index in this value as a long.
      Specified by:
      getLong in interface Bytes
      Parameters:
      i - The index from which to get the long, which must less than or equal to size() - 8.
      Returns:
      A long whose value is the 8 bytes from this value starting at index i.
    • toLong

      public long toLong()
      Description copied from interface: Bytes
      The value corresponding to interpreting these bytes as a long.
      Specified by:
      toLong in interface Bytes
      Returns:
      An value corresponding to this value interpreted as a long.
    • toBigInteger

      public BigInteger toBigInteger()
      Description copied from interface: Bytes
      The BigInteger corresponding to interpreting these bytes as a two's-complement signed integer.
      Specified by:
      toBigInteger in interface Bytes
      Returns:
      A BigInteger corresponding to interpreting these bytes as a two's-complement signed integer.
    • toUnsignedBigInteger

      public BigInteger toUnsignedBigInteger()
      Description copied from interface: Bytes
      The BigInteger corresponding to interpreting these bytes as an unsigned integer.
      Specified by:
      toUnsignedBigInteger in interface Bytes
      Returns:
      A positive (or zero) BigInteger corresponding to interpreting these bytes as an unsigned integer.
    • isZero

      public boolean isZero()
      Description copied from interface: Bytes
      Whether this value has only zero bytes.
      Specified by:
      isZero in interface Bytes
      Returns:
      true if all the bits of this value are zeros.
    • numberOfLeadingZeros

      public int numberOfLeadingZeros()
      Description copied from interface: Bytes
      Provides the number of zero bits preceding the highest-order ("leftmost") one-bit, or size() * 8 if all bits * are zero.
      Specified by:
      numberOfLeadingZeros in interface Bytes
      Returns:
      The number of zero bits preceding the highest-order ("leftmost") one-bit, or size() * 8 if all bits are zero.
    • numberOfLeadingZeroBytes

      public int numberOfLeadingZeroBytes()
      Description copied from interface: Bytes
      Provides the number of leading zero bytes of the value
      Specified by:
      numberOfLeadingZeroBytes in interface Bytes
      Returns:
      The number of leading zero bytes of the value.
    • hasLeadingZeroByte

      public boolean hasLeadingZeroByte()
      Description copied from interface: Bytes
      Whether the bytes start with a zero byte value.
      Specified by:
      hasLeadingZeroByte in interface Bytes
      Returns:
      true if the first byte equals zero
    • hasLeadingZero

      public boolean hasLeadingZero()
      Description copied from interface: Bytes
      Whether the bytes start with a zero bit value.
      Specified by:
      hasLeadingZero in interface Bytes
      Returns:
      true if the first bit equals zero
    • bitLength

      public int bitLength()
      Description copied from interface: Bytes
      Provides the number of bits following and including the highest-order ("leftmost") one-bit, or zero if all bits are zero.
      Specified by:
      bitLength in interface Bytes
      Returns:
      The number of bits following and including the highest-order ("leftmost") one-bit, or zero if all bits are zero.
    • and

      public Bytes and(Bytes other)
      Description copied from interface: Bytes
      Return a bit-wise AND of these bytes and the supplied bytes.

      If this value and the supplied value are different lengths, then the shorter will be zero-padded to the left.

      Specified by:
      and in interface Bytes
      Parameters:
      other - The bytes to perform the operation with.
      Returns:
      The result of a bit-wise AND.
    • and

      public <T extends MutableBytes> T and(Bytes other, T result)
      Description copied from interface: Bytes
      Calculate a bit-wise AND of these bytes and the supplied bytes.

      If this value or the supplied value are shorter in length than the output vector, then they will be zero-padded to the left. Likewise, if either this value or the supplied valid is longer in length than the output vector, then they will be truncated to the left.

      Specified by:
      and in interface Bytes
      Type Parameters:
      T - The MutableBytes value type.
      Parameters:
      other - The bytes to perform the operation with.
      result - The mutable output vector for the result.
      Returns:
      The result output vector.
    • or

      public Bytes or(Bytes other)
      Description copied from interface: Bytes
      Return a bit-wise OR of these bytes and the supplied bytes.

      If this value and the supplied value are different lengths, then the shorter will be zero-padded to the left.

      Specified by:
      or in interface Bytes
      Parameters:
      other - The bytes to perform the operation with.
      Returns:
      The result of a bit-wise OR.
    • or

      public <T extends MutableBytes> T or(Bytes other, T result)
      Description copied from interface: Bytes
      Calculate a bit-wise OR of these bytes and the supplied bytes.

      If this value or the supplied value are shorter in length than the output vector, then they will be zero-padded to the left. Likewise, if either this value or the supplied valid is longer in length than the output vector, then they will be truncated to the left.

      Specified by:
      or in interface Bytes
      Type Parameters:
      T - The MutableBytes value type.
      Parameters:
      other - The bytes to perform the operation with.
      result - The mutable output vector for the result.
      Returns:
      The result output vector.
    • xor

      public Bytes xor(Bytes other)
      Description copied from interface: Bytes
      Return a bit-wise XOR of these bytes and the supplied bytes.

      If this value and the supplied value are different lengths, then the shorter will be zero-padded to the left.

      Specified by:
      xor in interface Bytes
      Parameters:
      other - The bytes to perform the operation with.
      Returns:
      The result of a bit-wise XOR.
    • xor

      public <T extends MutableBytes> T xor(Bytes other, T result)
      Description copied from interface: Bytes
      Calculate a bit-wise XOR of these bytes and the supplied bytes.

      If this value or the supplied value are shorter in length than the output vector, then they will be zero-padded to the left. Likewise, if either this value or the supplied valid is longer in length than the output vector, then they will be truncated to the left.

      Specified by:
      xor in interface Bytes
      Type Parameters:
      T - The MutableBytes value type.
      Parameters:
      other - The bytes to perform the operation with.
      result - The mutable output vector for the result.
      Returns:
      The result output vector.
    • not

      public <T extends MutableBytes> T not(T result)
      Description copied from interface: Bytes
      Calculate a bit-wise NOT of these bytes.

      If this value is shorter in length than the output vector, then it will be zero-padded to the left. Likewise, if this value is longer in length than the output vector, then it will be truncated to the left.

      Specified by:
      not in interface Bytes
      Type Parameters:
      T - The MutableBytes value type.
      Parameters:
      result - The mutable output vector for the result.
      Returns:
      The result output vector.
    • shiftRight

      public <T extends MutableBytes> T shiftRight(int distance, T result)
      Description copied from interface: Bytes
      Shift all bits in this value to the right.

      If this value is shorter in length than the output vector, then it will be zero-padded to the left. Likewise, if this value is longer in length than the output vector, then it will be truncated to the left (after shifting).

      Specified by:
      shiftRight in interface Bytes
      Type Parameters:
      T - The MutableBytes value type.
      Parameters:
      distance - The number of bits to shift by.
      result - The mutable output vector for the result.
      Returns:
      The result output vector.
    • shiftLeft

      public <T extends MutableBytes> T shiftLeft(int distance, T result)
      Description copied from interface: Bytes
      Shift all bits in this value to the left.

      If this value is shorter in length than the output vector, then it will be zero-padded to the left. Likewise, if this value is longer in length than the output vector, then it will be truncated to the left.

      Specified by:
      shiftLeft in interface Bytes
      Type Parameters:
      T - The MutableBytes value type.
      Parameters:
      distance - The number of bits to shift by.
      result - The mutable output vector for the result.
      Returns:
      The result output vector.
    • slice

      public Bytes slice(int index)
      Description copied from interface: Bytes
      Create a new value representing (a view of) a slice of the bytes of this value.

      Please note that the resulting slice is only a view and as such maintains a link to the underlying full value. So holding a reference to the returned slice may hold more memory than the slide represents. Use Bytes.copy() on the returned slice if that is not what you want.

      Specified by:
      slice in interface Bytes
      Parameters:
      index - The start index for the slice.
      Returns:
      A new value providing a view over the bytes from index i (included) to the end.
    • slice

      public Bytes slice(int index, int length)
      Description copied from interface: Bytes
      Create a new value representing (a view of) a slice of the bytes of this value.

      Please note that the resulting slice is only a view and as such maintains a link to the underlying full value. So holding a reference to the returned slice may hold more memory than the slide represents. Use Bytes.copy() on the returned slice if that is not what you want.

      Specified by:
      slice in interface Bytes
      Parameters:
      index - The start index for the slice.
      length - The length of the resulting value.
      Returns:
      A new value providing a view over the bytes from index i (included) to i + length (excluded).
    • copy

      public Bytes copy()
      Description copied from interface: Bytes
      Return a value equivalent to this one but guaranteed to 1) be deeply immutable (i.e. the underlying value will be immutable) and 2) to not retain more bytes than exposed by the value.
      Specified by:
      copy in interface Bytes
      Returns:
      A value, equals to this one, but deeply immutable and that doesn't retain any "unreachable" bytes. For performance reasons, this is allowed to return this value however if it already fit those constraints.
    • mutableCopy

      public MutableBytes mutableCopy()
      Description copied from interface: Bytes
      Return a new mutable value initialized with the content of this value.
      Specified by:
      mutableCopy in interface Bytes
      Returns:
      A mutable copy of this value. This will copy bytes, modifying the returned value will not modify this value.
    • copyTo

      public void copyTo(MutableBytes destination)
      Description copied from interface: Bytes
      Copy the bytes of this value to the provided mutable one, which must have the same size.
      Specified by:
      copyTo in interface Bytes
      Parameters:
      destination - The mutable value to which to copy the bytes to, which must have the same size as this value. If you want to copy value where size differs, you should use Bytes.slice(int) and/or MutableBytes.mutableSlice(int, int) and apply the copy to the result.
    • copyTo

      public void copyTo(MutableBytes destination, int destinationOffset)
      Description copied from interface: Bytes
      Copy the bytes of this value to the provided mutable one from a particular offset.

      This is a (potentially slightly more efficient) shortcut for copyTo(destination.mutableSlice(destinationOffset, this.size())).

      Specified by:
      copyTo in interface Bytes
      Parameters:
      destination - The mutable value to which to copy the bytes to, which must have enough bytes from destinationOffset for the copied value.
      destinationOffset - The offset in destination at which the copy starts.
    • appendTo

      public void appendTo(ByteBuffer byteBuffer)
      Description copied from interface: Bytes
      Append the bytes of this value to the ByteBuffer.
      Specified by:
      appendTo in interface Bytes
      Parameters:
      byteBuffer - The ByteBuffer to which to append this value.
    • appendTo

      public void appendTo(io.vertx.core.buffer.Buffer buffer)
      Description copied from interface: Bytes
      Append the bytes of this value to the provided Vert.x Buffer.

      Note that since a Vert.x Buffer will grow as necessary, this method never fails.

      Specified by:
      appendTo in interface Bytes
      Parameters:
      buffer - The Buffer to which to append this value.
    • commonPrefixLength

      public int commonPrefixLength(Bytes other)
      Description copied from interface: Bytes
      Return the number of bytes in common between this set of bytes and another.
      Specified by:
      commonPrefixLength in interface Bytes
      Parameters:
      other - The bytes to compare to.
      Returns:
      The number of common bytes.
    • commonPrefix

      public Bytes commonPrefix(Bytes other)
      Description copied from interface: Bytes
      Return a slice over the common prefix between this set of bytes and another.
      Specified by:
      commonPrefix in interface Bytes
      Parameters:
      other - The bytes to compare to.
      Returns:
      A slice covering the common prefix.
    • update

      public void update(MessageDigest digest)
      Description copied from interface: Bytes
      Update the provided message digest with the bytes of this value.
      Specified by:
      update in interface Bytes
      Parameters:
      digest - The digest to update.
    • toArray

      public byte[] toArray()
      Description copied from interface: Bytes
      Extract the bytes of this value into a byte array.
      Specified by:
      toArray in interface Bytes
      Returns:
      A byte array with the same content than this value.
    • toArrayUnsafe

      public byte[] toArrayUnsafe()
      Description copied from interface: Bytes
      Get the bytes represented by this value as byte array.

      Contrarily to Bytes.toArray(), this may avoid allocating a new array and directly return the backing array of this value if said value is array backed and doing so is possible. As such, modifications to the returned array may or may not impact this value. As such, this method should be used with care and hence the "unsafe" moniker.

      Specified by:
      toArrayUnsafe in interface Bytes
      Returns:
      A byte array with the same content than this value, which may or may not be the direct backing of this value.
    • toString

      public String toString()
      Description copied from interface: Bytes
      Return the hexadecimal string representation of this value.
      Specified by:
      toString in interface Bytes
      Overrides:
      toString in class Object
      Returns:
      The hexadecimal representation of this value, starting with "0x".
    • toHexString

      public String toHexString()
      Description copied from interface: Bytes
      Provides this value represented as hexadecimal, starting with "0x".
      Specified by:
      toHexString in interface Bytes
      Returns:
      This value represented as hexadecimal, starting with "0x".
    • toShortHexString

      public String toShortHexString()
      Description copied from interface: Bytes
      Provides this value represented as a minimal hexadecimal string (without any leading zero)
      Specified by:
      toShortHexString in interface Bytes
      Returns:
      This value represented as a minimal hexadecimal string (without any leading zero).
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object