Class Bytes

java.lang.Object
org.apache.kafka.common.utils.Bytes
All Implemented Interfaces:
Comparable<Bytes>

public class Bytes extends Object implements Comparable<Bytes>
An immutable wrapper for a byte array.

This class provides a convenient way to work with byte arrays in Kafka APIs, particularly in Kafka Streams state stores and serialization. It implements Comparable to enable ordering of byte arrays.

The class caches the hashCode for improved performance when used as keys in hash-based data structures.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Deprecated, for removal: This API element is subject to removal in a future version.
    This interface is not part of the public API and will be removed in version 5.0.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    Deprecated, for removal: This API element is subject to removal in a future version.
    This field is not part of the public API and will be removed in version 5.0.
    static final byte[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Bytes(byte[] bytes)
    Create a Bytes using the byte array.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    boolean
    equals(Object other)
     
    byte[]
    get()
    Get the data from the Bytes.
    int
    The hashcode is cached except for the case where it is computed as 0, in which case we compute the hashcode on every call.
    static Bytes
    Deprecated, for removal: This API element is subject to removal in a future version.
    This method is not part of the public API and will be removed in version 5.0.
     
    static Bytes
    wrap(byte[] bytes)
    Creates a Bytes instance wrapping the given byte array.

    Methods inherited from class Object

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

    • EMPTY

      public static final byte[] EMPTY
    • BYTES_LEXICO_COMPARATOR

      @Deprecated(since="4.3", forRemoval=true) public static final Bytes.ByteArrayComparator BYTES_LEXICO_COMPARATOR
      Deprecated, for removal: This API element is subject to removal in a future version.
      This field is not part of the public API and will be removed in version 5.0. Internal Kafka code should use BytesUtils.BYTES_LEXICO_COMPARATOR instead.
      A byte array comparator based on lexicographic ordering.
  • Constructor Details

    • Bytes

      public Bytes(byte[] bytes)
      Create a Bytes using the byte array.
      Parameters:
      bytes - This array becomes the backing storage for the object.
      Throws:
      NullPointerException - if bytes is null
  • Method Details

    • wrap

      public static Bytes wrap(byte[] bytes)
      Creates a Bytes instance wrapping the given byte array.

      The provided array becomes the backing storage for the object.

      Parameters:
      bytes - the byte array to wrap, or null
      Returns:
      a new Bytes instance, or null if the input is null
    • get

      public byte[] get()
      Get the data from the Bytes.
      Returns:
      The underlying byte array
    • hashCode

      public int hashCode()
      The hashcode is cached except for the case where it is computed as 0, in which case we compute the hashcode on every call.
      Overrides:
      hashCode in class Object
      Returns:
      the hashcode
    • equals

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

      public int compareTo(Bytes that)
      Specified by:
      compareTo in interface Comparable<Bytes>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • increment

      @Deprecated(since="4.3", forRemoval=true) public static Bytes increment(Bytes input) throws IndexOutOfBoundsException
      Deprecated, for removal: This API element is subject to removal in a future version.
      This method is not part of the public API and will be removed in version 5.0. Internal Kafka code should use BytesUtils.increment(Bytes) instead.
      Increment the underlying byte array by adding 1.
      Parameters:
      input - - The byte array to increment
      Returns:
      A new copy of the incremented byte array.
      Throws:
      IndexOutOfBoundsException - if incrementing causes the underlying input byte array to overflow.