Class BytesUtils

java.lang.Object
org.apache.kafka.common.network.netty.BytesUtils

public class BytesUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    append(org.apache.kafka.common.record.internal.FileRecords fileRecords, ByteBuffer[] values)
    Appends ByteBuffer values to the FileRecords instance with offsets.
    static void
    appendUnaligned(org.apache.kafka.common.record.internal.FileRecords fileRecords, ByteBuffer[] values)
    Appends ByteBuffer values to the FileRecords instance without offsets.
    static org.apache.kafka.common.record.internal.FileRecords
    createFileRecords(ByteBuffer[] values, boolean offsetAligned)
    Creates a FileRecords instance with the specified ByteBuffer values.
    static ByteBuffer
    Generates a ByteBuffer of the specified size filled with random bytes.
    static ByteBuffer[]
    generateByteBuffers(int length, int minSizeInclusive, int maxSizeExclusive)
    Generates an array of ByteBuffers with random sizes.
    static byte[]
    merge(List<byte[]> byteArrays)
    Merges a list of byte arrays into a single byte array.
    static org.apache.kafka.common.network.ByteBufferSend
    Creates a ByteBufferSend instance containing a size-prefixed ByteBuffer representation of the given string.
    static byte[]
    Reads the contents of a file into a byte array.
    static byte[]
    Creates a size-prefixed byte array from the given string.
    static byte[]
    Read from ByteBuffer[] and write to byte[] without modifying the position of the ByteBuffer
    static byte[]
    toByteArray(Collection<io.netty.buffer.ByteBuf> byteBufs)
    Converts a collection of ByteBufs into a byte array.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • generateByteBuffers

      public static ByteBuffer[] generateByteBuffers(int length, int minSizeInclusive, int maxSizeExclusive)
      Generates an array of ByteBuffers with random sizes.
      Parameters:
      length - the number of ByteBuffers to generate
      minSizeInclusive - the minimum size of each ByteBuffer (inclusive)
      maxSizeExclusive - the maximum size of each ByteBuffer (exclusive)
      Returns:
      an array of ByteBuffers, each with a random size between minSizeInclusive and maxSizeExclusive
    • toByteArray

      public static byte[] toByteArray(Collection<io.netty.buffer.ByteBuf> byteBufs)
      Converts a collection of ByteBufs into a byte array. This method reads all the ByteBufs in the collection and combines them into a single byte array.
      Parameters:
      byteBufs - the collection of ByteBufs to convert
      Returns:
      a byte array containing the combined data from all ByteBufs
    • toByteArray

      public static byte[] toByteArray(ByteBuffer[] buffers)
      Read from ByteBuffer[] and write to byte[] without modifying the position of the ByteBuffer
    • generateByteBuffer

      public static ByteBuffer generateByteBuffer(int size)
      Generates a ByteBuffer of the specified size filled with random bytes.
      Parameters:
      size - the size of the ByteBuffer to generate
      Returns:
      a ByteBuffer filled with random bytes
    • createFileRecords

      public static org.apache.kafka.common.record.internal.FileRecords createFileRecords(ByteBuffer[] values, boolean offsetAligned) throws IOException
      Creates a FileRecords instance with the specified ByteBuffer values. If offsetAligned is true, the records are appended with offsets; otherwise, they are appended unaligned.
      Parameters:
      values - the ByteBuffer values to append
      offsetAligned - whether to append records with offsets
      Returns:
      a FileRecords instance containing the appended records
      Throws:
      IOException - if an I/O error occurs
    • append

      public static void append(org.apache.kafka.common.record.internal.FileRecords fileRecords, ByteBuffer[] values) throws IOException
      Appends ByteBuffer values to the FileRecords instance with offsets. Each value is wrapped in a MemoryRecordsBuilder and appended to the FileRecords.
      Parameters:
      fileRecords - the FileRecords instance to append to
      values - the ByteBuffer values to append
      Throws:
      IOException - if an I/O error occurs
    • appendUnaligned

      public static void appendUnaligned(org.apache.kafka.common.record.internal.FileRecords fileRecords, ByteBuffer[] values) throws IOException
      Appends ByteBuffer values to the FileRecords instance without offsets. Each value is wrapped in a SimpleRecord and appended to the FileRecords.
      Parameters:
      fileRecords - the FileRecords instance to append to
      values - the ByteBuffer values to append
      Throws:
      IOException - if an I/O error occurs
    • readFileToByteArray

      public static byte[] readFileToByteArray(File file) throws IOException
      Reads the contents of a file into a byte array.
      Parameters:
      file - the file to read
      Returns:
      a byte array containing the contents of the file
      Throws:
      IOException - if an I/O error occurs while reading the file
    • sizePrefixedBuffer

      public static byte[] sizePrefixedBuffer(String str)
      Creates a size-prefixed byte array from the given string. The resulting byte array contains the length of the string (as a 4-byte integer) followed by the UTF-8 encoded bytes of the string.
      Parameters:
      str - the input string
      Returns:
      a size-prefixed byte array representing the string
    • newByteBufferSend

      public static org.apache.kafka.common.network.ByteBufferSend newByteBufferSend(String str)
      Creates a ByteBufferSend instance containing a size-prefixed ByteBuffer representation of the given string.
      Parameters:
      str - the input string
      Returns:
      a ByteBufferSend instance with the size-prefixed ByteBuffer
    • merge

      public static byte[] merge(List<byte[]> byteArrays)
      Merges a list of byte arrays into a single byte array.
      Parameters:
      byteArrays - the list of byte arrays to merge
      Returns:
      a single byte array containing all the data from the input arrays