Class BytesUtils
java.lang.Object
org.apache.kafka.common.network.netty.BytesUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidappend(org.apache.kafka.common.record.internal.FileRecords fileRecords, ByteBuffer[] values) Appends ByteBuffer values to the FileRecords instance with offsets.static voidappendUnaligned(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.FileRecordscreateFileRecords(ByteBuffer[] values, boolean offsetAligned) Creates a FileRecords instance with the specified ByteBuffer values.static ByteBuffergenerateByteBuffer(int size) 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[]Merges a list of byte arrays into a single byte array.static org.apache.kafka.common.network.ByteBufferSendnewByteBufferSend(String str) Creates a ByteBufferSend instance containing a size-prefixed ByteBuffer representation of the given string.static byte[]readFileToByteArray(File file) Reads the contents of a file into a byte array.static byte[]sizePrefixedBuffer(String str) Creates a size-prefixed byte array from the given string.static byte[]toByteArray(ByteBuffer[] buffers) Read from ByteBuffer[] and write to byte[] without modifying the position of the ByteBufferstatic byte[]toByteArray(Collection<io.netty.buffer.ByteBuf> byteBufs) Converts a collection of ByteBufs into a byte array.
-
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 generateminSizeInclusive- 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
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
Read from ByteBuffer[] and write to byte[] without modifying the position of the ByteBuffer -
generateByteBuffer
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 appendoffsetAligned- 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 tovalues- 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 tovalues- the ByteBuffer values to append- Throws:
IOException- if an I/O error occurs
-
readFileToByteArray
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
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
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
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
-