Interface DataBuffer
- All Known Subinterfaces:
PooledDataBuffer
- All Known Implementing Classes:
DataBufferWrapper
,DefaultDataBuffer
,NettyDataBuffer
public interface DataBuffer
Basic abstraction over byte buffers.DataBuffer
s has a separate read and write position, as opposed toByteBuffer
's single position. As such, theDataBuffer
does not require a flip to read after writing. In general, the following invariant holds for the read and write positions, and the capacity:0<=readPosition<=writePosition<=capacity
The capacity of a
DataBuffer
is expanded on demand, similar toStringBuilder
.The main purpose of the
DataBuffer
abstraction is to provide a convenient wrapper aroundByteBuffer
which is similar to Netty'sByteBuf
but can also be used on non-Netty platforms (i.e. Servlet containers).- Since:
- 5.0
- Author:
- Arjen Poutsma, Brian Clozel
- See Also:
DataBufferFactory
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description ByteBuffer
asByteBuffer()
Expose this buffer's bytes as aByteBuffer
.ByteBuffer
asByteBuffer(int index, int length)
Expose a subsequence of this buffer's bytes as aByteBuffer
.InputStream
asInputStream()
Expose this buffer's data as anInputStream
.InputStream
asInputStream(boolean releaseOnClose)
Expose this buffer's data as anInputStream
.OutputStream
asOutputStream()
Expose this buffer's data as anOutputStream
.int
capacity()
Return the number of bytes that this buffer can contain.DataBuffer
capacity(int capacity)
Set the number of bytes that this buffer can contain.default DataBuffer
ensureCapacity(int capacity)
Ensure that the current buffer has enoughwritableByteCount()
to write the amount of data given as an argument.DataBufferFactory
factory()
Return theDataBufferFactory
that created this buffer.byte
getByte(int index)
Read a single byte at the given index from this data buffer.int
indexOf(IntPredicate predicate, int fromIndex)
Return the index of the first byte in this buffer that matches the given predicate.int
lastIndexOf(IntPredicate predicate, int fromIndex)
Return the index of the last byte in this buffer that matches the given predicate.byte
read()
Read a single byte from the current reading position from this data buffer.DataBuffer
read(byte[] destination)
Read this buffer's data into the specified destination, starting at the current reading position of this buffer.DataBuffer
read(byte[] destination, int offset, int length)
Read at mostlength
bytes of this buffer into the specified destination, starting at the current reading position of this buffer.int
readableByteCount()
Return the number of bytes that can be read from this data buffer.int
readPosition()
Return the position from which this buffer will read.DataBuffer
readPosition(int readPosition)
Set the position from which this buffer will read.default DataBuffer
retainedSlice(int index, int length)
Create a newDataBuffer
whose contents is a shared, retained subsequence of this data buffer's content.DataBuffer
slice(int index, int length)
Create a newDataBuffer
whose contents is a shared subsequence of this data buffer's content.String
toString(int index, int length, Charset charset)
Return a part of this buffer's data as a String using the specified charset.default String
toString(Charset charset)
Return this buffer's data a String using the specified charset.int
writableByteCount()
Return the number of bytes that can be written to this data buffer.DataBuffer
write(byte b)
Write a single byte into this buffer at the current writing position.DataBuffer
write(byte[] source)
Write the given source into this buffer, starting at the current writing position of this buffer.DataBuffer
write(byte[] source, int offset, int length)
Write at mostlength
bytes of the given source into this buffer, starting at the current writing position of this buffer.default DataBuffer
write(CharSequence charSequence, Charset charset)
Write the givenCharSequence
using the givenCharset
, starting at the current writing position.DataBuffer
write(ByteBuffer... buffers)
Write one or moreByteBuffer
to this buffer, starting at the current writing position.DataBuffer
write(DataBuffer... buffers)
Write one or moreDataBuffer
s to this buffer, starting at the current writing position.int
writePosition()
Return the position to which this buffer will write.DataBuffer
writePosition(int writePosition)
Set the position to which this buffer will write.
Method Detail
factory
DataBufferFactory factory()
Return theDataBufferFactory
that created this buffer.- Returns:
- the creating buffer factory
indexOf
int indexOf(IntPredicate predicate, int fromIndex)
Return the index of the first byte in this buffer that matches the given predicate.- Parameters:
predicate
- the predicate to matchfromIndex
- the index to start the search from- Returns:
- the index of the first byte that matches
predicate
; or-1
if none match
lastIndexOf
int lastIndexOf(IntPredicate predicate, int fromIndex)
Return the index of the last byte in this buffer that matches the given predicate.- Parameters:
predicate
- the predicate to matchfromIndex
- the index to start the search from- Returns:
- the index of the last byte that matches
predicate
; or-1
if none match
readableByteCount
int readableByteCount()
Return the number of bytes that can be read from this data buffer.- Returns:
- the readable byte count
writableByteCount
int writableByteCount()
Return the number of bytes that can be written to this data buffer.- Returns:
- the writable byte count
- Since:
- 5.0.1
capacity
int capacity()
Return the number of bytes that this buffer can contain.- Returns:
- the capacity
- Since:
- 5.0.1
capacity
DataBuffer capacity(int capacity)
Set the number of bytes that this buffer can contain.If the new capacity is lower than the current capacity, the contents of this buffer will be truncated. If the new capacity is higher than the current capacity, it will be expanded.
- Parameters:
capacity
- the new capacity- Returns:
- this buffer
ensureCapacity
default DataBuffer ensureCapacity(int capacity)
Ensure that the current buffer has enoughwritableByteCount()
to write the amount of data given as an argument. If not, the missing capacity will be added to the buffer.- Parameters:
capacity
- the writable capacity to check for- Returns:
- this buffer
- Since:
- 5.1.4
readPosition
int readPosition()
Return the position from which this buffer will read.- Returns:
- the read position
- Since:
- 5.0.1
readPosition
DataBuffer readPosition(int readPosition)
Set the position from which this buffer will read.- Parameters:
readPosition
- the new read position- Returns:
- this buffer
- Throws:
IndexOutOfBoundsException
- ifreadPosition
is smaller than 0 or greater thanwritePosition()
- Since:
- 5.0.1
writePosition
int writePosition()
Return the position to which this buffer will write.- Returns:
- the write position
- Since:
- 5.0.1
writePosition
DataBuffer writePosition(int writePosition)
Set the position to which this buffer will write.- Parameters:
writePosition
- the new write position- Returns:
- this buffer
- Throws:
IndexOutOfBoundsException
- ifwritePosition
is smaller thanreadPosition()
or greater thancapacity()
- Since:
- 5.0.1
getByte
byte getByte(int index)
Read a single byte at the given index from this data buffer.- Parameters:
index
- the index at which the byte will be read- Returns:
- the byte at the given index
- Throws:
IndexOutOfBoundsException
- whenindex
is out of bounds- Since:
- 5.0.4
read
byte read()
Read a single byte from the current reading position from this data buffer.- Returns:
- the byte at this buffer's current reading position
read
DataBuffer read(byte[] destination)
Read this buffer's data into the specified destination, starting at the current reading position of this buffer.- Parameters:
destination
- the array into which the bytes are to be written- Returns:
- this buffer
read
DataBuffer read(byte[] destination, int offset, int length)
Read at mostlength
bytes of this buffer into the specified destination, starting at the current reading position of this buffer.- Parameters:
destination
- the array into which the bytes are to be writtenoffset
- the index withindestination
of the first byte to be writtenlength
- the maximum number of bytes to be written indestination
- Returns:
- this buffer
write
DataBuffer write(byte b)
Write a single byte into this buffer at the current writing position.- Parameters:
b
- the byte to be written- Returns:
- this buffer
write
DataBuffer write(byte[] source)
Write the given source into this buffer, starting at the current writing position of this buffer.- Parameters:
source
- the bytes to be written into this buffer- Returns:
- this buffer
write
DataBuffer write(byte[] source, int offset, int length)
Write at mostlength
bytes of the given source into this buffer, starting at the current writing position of this buffer.- Parameters:
source
- the bytes to be written into this bufferoffset
- the index withinsource
to start writing fromlength
- the maximum number of bytes to be written fromsource
- Returns:
- this buffer
write
DataBuffer write(DataBuffer... buffers)
Write one or moreDataBuffer
s to this buffer, starting at the current writing position. It is the responsibility of the caller to release the given data buffers.- Parameters:
buffers
- the byte buffers to write into this buffer- Returns:
- this buffer
write
DataBuffer write(ByteBuffer... buffers)
Write one or moreByteBuffer
to this buffer, starting at the current writing position.- Parameters:
buffers
- the byte buffers to write into this buffer- Returns:
- this buffer
write
default DataBuffer write(CharSequence charSequence, Charset charset)
Write the givenCharSequence
using the givenCharset
, starting at the current writing position.- Parameters:
charSequence
- the char sequence to write into this buffercharset
- the charset to encode the char sequence with- Returns:
- this buffer
- Since:
- 5.1.4
slice
DataBuffer slice(int index, int length)
Create a newDataBuffer
whose contents is a shared subsequence of this data buffer's content. Data between this data buffer and the returned buffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.Note that this method will not call
DataBufferUtils.retain(DataBuffer)
on the resulting slice: the reference count will not be increased.- Parameters:
index
- the index at which to start the slicelength
- the length of the slice- Returns:
- the specified slice of this data buffer
retainedSlice
default DataBuffer retainedSlice(int index, int length)
Create a newDataBuffer
whose contents is a shared, retained subsequence of this data buffer's content. Data between this data buffer and the returned buffer is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.Note that unlike
slice(int, int)
, this method will callDataBufferUtils.retain(DataBuffer)
(or equivalent) on the resulting slice.- Parameters:
index
- the index at which to start the slicelength
- the length of the slice- Returns:
- the specified, retained slice of this data buffer
- Since:
- 5.2
asByteBuffer
ByteBuffer asByteBuffer()
Expose this buffer's bytes as aByteBuffer
. Data between thisDataBuffer
and the returnedByteBuffer
is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.- Returns:
- this data buffer as a byte buffer
asByteBuffer
ByteBuffer asByteBuffer(int index, int length)
Expose a subsequence of this buffer's bytes as aByteBuffer
. Data between thisDataBuffer
and the returnedByteBuffer
is shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.- Parameters:
index
- the index at which to start the byte bufferlength
- the length of the returned byte buffer- Returns:
- this data buffer as a byte buffer
- Since:
- 5.0.1
asInputStream
InputStream asInputStream()
Expose this buffer's data as anInputStream
. Both data and read position are shared between the returned stream and this data buffer. The underlying buffer will not be released when the input stream is closed.- Returns:
- this data buffer as an input stream
- See Also:
asInputStream(boolean)
asInputStream
InputStream asInputStream(boolean releaseOnClose)
Expose this buffer's data as anInputStream
. Both data and read position are shared between the returned stream and this data buffer.
asOutputStream
OutputStream asOutputStream()
Expose this buffer's data as anOutputStream
. Both data and write position are shared between the returned stream and this data buffer.- Returns:
- this data buffer as an output stream
toString
default String toString(Charset charset)
Return this buffer's data a String using the specified charset. Default implementation delegates totoString(readPosition(), readableByteCount(), charset)
.- Parameters:
charset
- the character set to use- Returns:
- a string representation of all this buffers data
- Since:
- 5.2
toString
String toString(int index, int length, Charset charset)
Return a part of this buffer's data as a String using the specified charset.- Parameters:
index
- the index at which to start the stringlength
- the number of bytes to use for the stringcharset
- the charset to use- Returns:
- a string representation of a part of this buffers data
- Since:
- 5.2