- All Known Subinterfaces:
CompositeBufferData
public interface BufferData
Wrapper around a byte array.
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Number of bytes available for reading.int
capacity()
Number of bytes that can be written to this instance.clear()
Reset read and write position and make the buffer empty.boolean
consumed()
Whether this buffer is fully consumed (all available bytes were read).default BufferData
copy()
Copy the underlying data into a new buffer that does not retain any reference.static BufferData
create
(byte[] bytes) Fixed size buffer data fully written.static BufferData
create
(byte[] bytes, int position, int length) Fixed size buffer data fully written.static BufferData
create
(int length) Fixed size buffer data.static BufferData
create
(BufferData... data) Buffer data mapping multiple buffers.static BufferData
Create buffer data from a string.static BufferData
create
(List<BufferData> data) Create composite buffer data from a list.static CompositeBufferData
Composite buffer data that are mutable.static CompositeBufferData
createComposite
(BufferData first) Composite buffer data that are mutable with initial value.static BufferData
createReadOnly
(byte[] bytes, int offset, int length) Fixed size buffer data fully written.Debug this buffer as binary string.default String
Debug the full buffer.debugDataHex
(boolean fullBuffer) Debug this buffer as hex string.static BufferData
empty()
Empty buffer data.default void
Do an operation on each byte in this buffer.int
get
(int index) Get byte at index (current read position + index).static BufferData
growing
(int initialLength) Growing buffer data.int
indexOf
(byte aByte) Find index of the provided byte from the current position.default int
lastIndexOf
(byte aByte) Find last index of the provided byte from the current position.int
lastIndexOf
(byte aByte, int length) Find last index of the provided byte from the current position.int
read()
Read a single byte from this buffer.default int
read
(byte[] bytes) Read bytes from this buffer into the provided buffer.int
read
(byte[] bytes, int position, int length) Read bytes from this buffer into the provided buffer.default byte[]
Read the content of this data as bytes.int
readFrom
(InputStream in) Read bytes from the input stream.default int
readHpackInt
(int originalValue, int bitsOfPrefix) HPack integer value (may be 1 or more bytes).default int
Read 16-bit integer.default int
Read a 24-bit integer.default int
Read 32-bit integer.default long
readLong()
Read 64-bit long.default String
readString
(int length) Read a UTF-8 string from this buffer.readString
(int length, Charset charset) Read a string from this buffer.default long
Read 32-bit unsigned integer (must be represented as a long, as Java integer is 32-bit signed).reset()
Reset read and write position of this buffer.rewind()
Set read position to 0, so others may read the same data from the start.void
skip
(int length) Skip the next n bytes (move read position).static String
toBinaryString
(int value) Integer to binary string.static int
toInt31
(int value) Get 31 bits of an integer, ignoring the first bit.trim
(int x) Trim the last x bytes from a buffer (remove them).default void
write
(byte[] bytes) Write the byte array to this buffer.void
write
(byte[] bytes, int offset, int length) Write the byte array to this buffer.write
(int value) Write a byte.default void
write
(BufferData toWrite) Write the provided buffer to this buffer.void
write
(BufferData toWrite, int length) Write n bytes from the provided buffer to this buffer.default void
writeAscii
(String text) Write ascii string to this buffer.default BufferData
writeHpackInt
(int value, int prefixedInt, int bitPrefix) Write hpack integer to this buffer.default BufferData
writeInt16
(int number) Write 16-bit integer.default BufferData
writeInt24
(int number) Write 24-bit integer.default BufferData
writeInt32
(int number) Write 32-bit integer.default BufferData
writeInt8
(int number) Write 8-bit integer.void
writeTo
(OutputStream out) Write all available bytes of this buffer to the output stream.int
writeTo
(ByteBuffer writeBuffer, int length) Write n bytes from this buffer to the provided buffer.default BufferData
writeUnsignedInt32
(long number) Write 32-bit unsigned integer.
-
Field Details
-
EMPTY_BYTES
static final byte[] EMPTY_BYTESEmpty byte array.
-
-
Method Details
-
create
Fixed size buffer data.- Parameters:
length
- length of the underlying buffer- Returns:
- new byte array buffer data
-
create
Fixed size buffer data fully written.- Parameters:
bytes
- byte array- Returns:
- new byte array buffer data with write position moved to the last byte
-
create
Fixed size buffer data fully written.- Parameters:
bytes
- byte arrayposition
- position within the byte arraylength
- number of bytes from position that contain the data- Returns:
- new byte array buffer data read to be read
-
createReadOnly
Fixed size buffer data fully written.- Parameters:
bytes
- byte arrayoffset
- offset within the byte arraylength
- length- Returns:
- new byte array buffer data that are read only
-
growing
Growing buffer data. The buffer will grow when necessary to accommodate more bytes.- Parameters:
initialLength
- initial buffer length- Returns:
- growing buffer data
-
create
Buffer data mapping multiple buffers.- Parameters:
data
- data to wrap- Returns:
- composite buffer data
-
createComposite
Composite buffer data that are mutable.- Returns:
- composite buffer
-
createComposite
Composite buffer data that are mutable with initial value.- Parameters:
first
- first buffer to be added to the composite buffer- Returns:
- composite buffer
-
create
Create composite buffer data from a list.- Parameters:
data
- list of buffers to use- Returns:
- composite buffer
-
toBinaryString
Integer to binary string.- Parameters:
value
- integer to print as binary- Returns:
- binary value
-
toInt31
static int toInt31(int value) Get 31 bits of an integer, ignoring the first bit.- Parameters:
value
- integer value (32 bits)- Returns:
- integer value (31 bits)
-
empty
Empty buffer data.- Returns:
- empty buffer
-
create
Create buffer data from a string.- Parameters:
stringValue
- UTF-8 string- Returns:
- buffer data with bytes of the string
-
reset
BufferData reset()Reset read and write position of this buffer. Does not impact length.- Returns:
- this instance
-
rewind
BufferData rewind()Set read position to 0, so others may read the same data from the start.- Returns:
- this instance
-
clear
BufferData clear()Reset read and write position and make the buffer empty. Growing buffer length is set to 0- Returns:
- this instance
-
writeTo
Write all available bytes of this buffer to the output stream.- Parameters:
out
- output stream
-
readFrom
Read bytes from the input stream. Reads at least 1 byte.- Parameters:
in
- input stream- Returns:
- number of bytes read, -1 if the input stream is finished
-
read
int read()Read a single byte from this buffer.- Returns:
- next byte
-
read
default int read(byte[] bytes) Read bytes from this buffer into the provided buffer.- Parameters:
bytes
- buffer to write to- Returns:
- number of bytes actually written to the provided buffer
-
read
int read(byte[] bytes, int position, int length) Read bytes from this buffer into the provided buffer.- Parameters:
bytes
- buffer to write toposition
- position in the bufferlength
- length that can be written- Returns:
- actual number of bytes written
-
forEach
Do an operation on each byte in this buffer.- Parameters:
length
- number of bytes to go throughconsumer
- function that consumes a byte and returnstrue
if we should proceed to the next byte
-
readString
Read a UTF-8 string from this buffer.- Parameters:
length
- number of bytes to read- Returns:
- string from the bytes
-
readString
Read a string from this buffer.- Parameters:
length
- number of bytes to readcharset
- charset of the string, such asStandardCharsets.UTF_8
orStandardCharsets.US_ASCII
- Returns:
- string from the bytes
-
consumed
boolean consumed()Whether this buffer is fully consumed (all available bytes were read).- Returns:
- if this buffer is consumed
-
readInt16
default int readInt16()Read 16-bit integer.- Returns:
- integer from the next 2 bytes
-
readInt24
default int readInt24()Read a 24-bit integer.- Returns:
- integer from the next 3 bytes
-
readInt32
default int readInt32()Read 32-bit integer.- Returns:
- integer from the next 4 bytes
-
readUnsignedInt32
default long readUnsignedInt32()Read 32-bit unsigned integer (must be represented as a long, as Java integer is 32-bit signed).- Returns:
- long from the next 4 bytes
-
readLong
default long readLong()Read 64-bit long.- Returns:
- long from the next 8 bytes
-
writeInt8
Write 8-bit integer.- Parameters:
number
- integer to write as a single byte- Returns:
- this buffer
-
writeInt16
Write 16-bit integer.- Parameters:
number
- integer to write as 2 bytes- Returns:
- this buffer
-
writeInt24
Write 24-bit integer.- Parameters:
number
- integer to write as 3 bytes- Returns:
- this buffer
-
writeInt32
Write 32-bit integer.- Parameters:
number
- integer to write as 4 bytes- Returns:
- this buffer
-
writeUnsignedInt32
Write 32-bit unsigned integer.- Parameters:
number
- long to write as 4 bytes- Returns:
- this buffer
-
write
Write a byte.- Parameters:
value
- value- Returns:
- this buffer
-
writeTo
Write n bytes from this buffer to the provided buffer.- Parameters:
writeBuffer
- buffer to write tolength
- number of bytes to write- Returns:
- number of bytes actually written
-
write
default void write(byte[] bytes) Write the byte array to this buffer.- Parameters:
bytes
- byte to write
-
write
void write(byte[] bytes, int offset, int length) Write the byte array to this buffer.- Parameters:
bytes
- bytes to writeoffset
- offset within the arraylength
- number of bytes to write (from the offset)
-
write
Write the provided buffer to this buffer.- Parameters:
toWrite
- buffer to write
-
write
Write n bytes from the provided buffer to this buffer.- Parameters:
toWrite
- buffer to writelength
- number of bytes to write
-
readHpackInt
default int readHpackInt(int originalValue, int bitsOfPrefix) HPack integer value (may be 1 or more bytes). TODO enforce limit that the hpack int is max 4 bytes (as otherwise we overflow int and this may be an attack) TODO enforce limit to string values (not here, but on headers processing)- Parameters:
originalValue
- value (only bitsOfPrefix are used, bits before that are ignored)bitsOfPrefix
- number of bits significant in the value- Returns:
- integer value
-
writeHpackInt
Write hpack integer to this buffer.- Parameters:
value
- the full value we want to writeprefixedInt
- value to store in the other bits of the first bytebitPrefix
- bits reserved for our value in the first byte- Returns:
- this instance
-
debugDataBinary
String debugDataBinary()Debug this buffer as binary string.- Returns:
- binary string debug data (including headers)
-
debugDataHex
Debug this buffer as hex string.- Parameters:
fullBuffer
- whether to debug all bytes in this buffer (true
), or only unread bytesfalse
)- Returns:
- hex debug data (including headers)
-
debugDataHex
Debug the full buffer.- Returns:
- hex debug data (including headers)
-
copy
Copy the underlying data into a new buffer that does not retain any reference. Reads this buffer fully and creates a new instance that is not completed.- Returns:
- copy of data
-
available
int available()Number of bytes available for reading.- Returns:
- available bytes
-
skip
void skip(int length) Skip the next n bytes (move read position).- Parameters:
length
- number of bytes to skip
-
indexOf
int indexOf(byte aByte) Find index of the provided byte from the current position.- Parameters:
aByte
- byte to find- Returns:
- index of the byte, or
-1
if not found
-
lastIndexOf
default int lastIndexOf(byte aByte) Find last index of the provided byte from the current position.- Parameters:
aByte
- byte to find- Returns:
- index of the byte, or
-1
if not found
-
lastIndexOf
int lastIndexOf(byte aByte, int length) Find last index of the provided byte from the current position.- Parameters:
aByte
- byte to findlength
- maximal length to search for (e.g. this will be the last byte before reaching the length)- Returns:
- index of the byte, or
-1
if not found
-
trim
Trim the last x bytes from a buffer (remove them).- Parameters:
x
- trim by this number of bytes- Returns:
- this instance
-
capacity
int capacity()Number of bytes that can be written to this instance.- Returns:
- capacity of this buffer
-
writeAscii
Write ascii string to this buffer.- Parameters:
text
- ascii string to write
-
get
int get(int index) Get byte at index (current read position + index). Does not modify any position.- Parameters:
index
- index to get- Returns:
- byte at the index
-
readBytes
default byte[] readBytes()Read the content of this data as bytes. This method always creates a new byte array.- Returns:
- byte array with
available()
bytes, may be empty
-