Class BytesRef

java.lang.Object
io.github.jbellis.jvector.util.BytesRef
All Implemented Interfaces:
Cloneable, Comparable<BytesRef>

public final class BytesRef extends Object implements Comparable<BytesRef>, Cloneable
Represents byte[], as a slice (offset + length) into an existing byte[]. The bytes member should never be null; use EMPTY_BYTES if necessary.

BytesRef implements Comparable. The underlying byte arrays are sorted lexicographically, numerically treating elements as unsigned. This is identical to Unicode codepoint order.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    byte[]
    The contents of the BytesRef.
    static final byte[]
    An empty byte array for convenience
    int
    Length of used bytes.
    int
    Offset of first valid byte.
  • Constructor Summary

    Constructors
    Constructor
    Description
    BytesRef(byte[] bytes)
    This instance will directly reference bytes w/o making a copy.
    BytesRef(byte[] bytes, int offset, int length)
    This instance will directly reference bytes w/o making a copy.
    BytesRef(int capacity)
    Create a BytesRef pointing to a new array of size capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Expert: compares the bytes against another BytesRef, returning true if the bytes are equal.
    Returns a shallow clone of this instance (the underlying bytes are not copied and will be shared by both the returned object and this object.
    int
    Unsigned byte order comparison
    boolean
    equals(Object other)
     
    boolean
    Performs internal consistency checks.
    Returns hex encoded bytes, eg [0x6c 0x75 0x63 0x65 0x6e 0x65]

    Methods inherited from class java.lang.Object

    finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • EMPTY_BYTES

      public static final byte[] EMPTY_BYTES
      An empty byte array for convenience
    • bytes

      public byte[] bytes
      The contents of the BytesRef. Should never be null.
    • offset

      public int offset
      Offset of first valid byte.
    • length

      public int length
      Length of used bytes.
  • Constructor Details

    • BytesRef

      public BytesRef(byte[] bytes, int offset, int length)
      This instance will directly reference bytes w/o making a copy. bytes should not be null.
    • BytesRef

      public BytesRef(byte[] bytes)
      This instance will directly reference bytes w/o making a copy. bytes should not be null
    • BytesRef

      public BytesRef(int capacity)
      Create a BytesRef pointing to a new array of size capacity. Offset and length will both be zero.
  • Method Details

    • bytesEquals

      public boolean bytesEquals(BytesRef other)
      Expert: compares the bytes against another BytesRef, returning true if the bytes are equal.
      Parameters:
      other - Another BytesRef, should not be null.
    • clone

      public BytesRef clone()
      Returns a shallow clone of this instance (the underlying bytes are not copied and will be shared by both the returned object and this object.
      Overrides:
      clone in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Returns hex encoded bytes, eg [0x6c 0x75 0x63 0x65 0x6e 0x65]
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(BytesRef other)
      Unsigned byte order comparison
      Specified by:
      compareTo in interface Comparable<BytesRef>
    • isValid

      public boolean isValid()
      Performs internal consistency checks. Always returns true (or throws IllegalStateException)