java.lang.Object
net.i2p.data.ByteArray
All Implemented Interfaces:
Serializable, Comparable<ByteArray>
Direct Known Subclasses:
MsgID, Token

public class ByteArrayextends Object implements Serializable, Comparable<ByteArray>
Wrap up an array of bytes so that they can be compared and placed in hashes, maps, and the like.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private byte[]
    Underlying byte array data.
    private int
    Offset into the byte array where valid data begins.
    private int
    Number of valid bytes in the array.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty ByteArray.
    ByteArray(byte[] data)
    Sets valid = data.length, unless data is null Sets offset = 0.
    ByteArray(byte[] data, int offset, int length)
    Wrap the given range of a byte array.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static final boolean
    compare(byte[] lhs, int loff, int llen, byte[] rhs, int roff, int rlen)
    True when the two ranges hold the same bytes.
    final int
    Compares this ByteArray with another ByteArray.
    final boolean
    Compares this ByteArray with another object for equality.
    byte[]
    Returns the underlying byte array.
    int
    Returns the offset into the byte array.
    int
    Count how many of the bytes in the array are 'valid'.
    final int
    Hash code based on the underlying byte array data.
    void
    setData(byte[] data)
    Sets the underlying byte array.
    void
    setOffset(int offset)
    Offset into the byte array.
    void
    setValid(int valid)
    Number of valid bytes.
    final String
    Returns this ByteArray as a Base64 encoded string.
    Returns a string representation of this ByteArray.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • _data

      private byte[] _data
      Underlying byte array data.
    • _valid

      private int _valid
      Number of valid bytes in the array.
    • _offset

      private int _offset
      Offset into the byte array where valid data begins.
  • Constructor Details

    • ByteArray

      public ByteArray()
      Creates an empty ByteArray.
    • ByteArray

      public ByteArray(byte[] data)
      Sets valid = data.length, unless data is null Sets offset = 0.
      Parameters:
      data - the byte array, may be null
    • ByteArray

      public ByteArray(byte[] data, int offset, int length)
      Wrap the given range of a byte array.
      Parameters:
      data - the byte array, may be null but why would you do that
      offset - the starting offset
      length - the valid length
  • Method Details

    • getData

      public byte[] getData()
      Returns the underlying byte array.
      Returns:
      the backing array itself, not a copy; mutating it changes what this object holds
    • setData

      public void setData(byte[] data)
      Sets the underlying byte array. Warning, does not set valid.
      Parameters:
      data - the new byte array
    • getValid

      public int getValid()
      Count how many of the bytes in the array are 'valid'. this property does not necessarily have meaning for all byte arrays.
      Returns:
      the number of valid bytes
    • setValid

      public void setValid(int valid)
      Number of valid bytes.
      Parameters:
      valid - the new valid count
    • getOffset

      public int getOffset()
      Returns the offset into the byte array.
      Returns:
      the index of the first live byte within getData()
    • setOffset

      public void setOffset(int offset)
      Offset into the byte array.
      Parameters:
      offset - the new offset
    • equals

      public final boolean equals(Object o)
      Compares this ByteArray with another object for equality.
      Overrides:
      equals in class Object
    • compare

      private static final boolean compare(byte[] lhs, int loff, int llen, byte[] rhs, int roff, int rlen)
      True when the two ranges hold the same bytes.
      Parameters:
      lhs - the first array
      loff - where its range starts
      llen - its range length
      rhs - the second array
      roff - where its range starts
      rlen - its range length
      Returns:
      true if the ranges are equal
    • compareTo

      public final int compareTo(ByteArray ba)
      Compares this ByteArray with another ByteArray.
      Specified by:
      compareTo in interface Comparable<ByteArray>
    • hashCode

      public final int hashCode()
      Hash code based on the underlying byte array data.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a string representation of this ByteArray.
      Overrides:
      toString in class Object
    • toBase64

      public final String toBase64()
      Returns this ByteArray as a Base64 encoded string.
      Returns:
      the Base64 encoded string