public interface DataBuffer
DataBuffer
s has a separate read and
write position, as opposed to ByteBuffer
's
single position. As such, the DataBuffer
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 to StringBuilder
.
The main purpose of the DataBuffer
abstraction is to provide a convenient wrapper
around ByteBuffer
which is similar to Netty's ByteBuf
but
can also be used on non-Netty platforms (i.e. Servlet containers).
DataBufferFactory
Modifier and Type | Method and Description |
---|---|
ByteBuffer |
asByteBuffer()
Expose this buffer's bytes as a
ByteBuffer . |
ByteBuffer |
asByteBuffer(int index,
int length)
Expose a subsequence of this buffer's bytes as a
ByteBuffer . |
InputStream |
asInputStream()
Expose this buffer's data as an
InputStream . |
InputStream |
asInputStream(boolean releaseOnClose)
Expose this buffer's data as an
InputStream . |
OutputStream |
asOutputStream()
Expose this buffer's data as an
OutputStream . |
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 enough
writableByteCount()
to write the amount of data given as an argument. |
DataBufferFactory |
factory()
Return the
DataBufferFactory 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 most
length 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 new
DataBuffer whose contents is a shared, retained subsequence of this
data buffer's content. |
DataBuffer |
slice(int index,
int length)
Create a new
DataBuffer whose contents is a shared subsequence of this
data buffer's content. |
default String |
toString(Charset charset)
Return this buffer's data a String using the specified charset.
|
String |
toString(int index,
int length,
Charset charset)
Return a part of this buffer's data as 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 most
length bytes of the given source into this buffer, starting
at the current writing position of this buffer. |
DataBuffer |
write(ByteBuffer... buffers)
Write one or more
ByteBuffer to this buffer, starting at the current
writing position. |
default DataBuffer |
write(CharSequence charSequence,
Charset charset)
Write the given
CharSequence using the given Charset ,
starting at the current writing position. |
DataBuffer |
write(DataBuffer... buffers)
Write one or more
DataBuffer 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.
|
DataBufferFactory factory()
DataBufferFactory
that created this buffer.int indexOf(IntPredicate predicate, int fromIndex)
predicate
- the predicate to matchfromIndex
- the index to start the search frompredicate
;
or -1
if none matchint lastIndexOf(IntPredicate predicate, int fromIndex)
predicate
- the predicate to matchfromIndex
- the index to start the search frompredicate
;
or -1
if none matchint readableByteCount()
int writableByteCount()
int capacity()
DataBuffer capacity(int capacity)
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.
capacity
- the new capacitydefault DataBuffer ensureCapacity(int capacity)
writableByteCount()
to write the amount of data given as an argument. If not, the missing
capacity will be added to the buffer.capacity
- the writable capacity to check forint readPosition()
DataBuffer readPosition(int readPosition)
readPosition
- the new read positionIndexOutOfBoundsException
- if readPosition
is smaller than 0
or greater than writePosition()
int writePosition()
DataBuffer writePosition(int writePosition)
writePosition
- the new write positionIndexOutOfBoundsException
- if writePosition
is smaller than
readPosition()
or greater than capacity()
byte getByte(int index)
index
- the index at which the byte will be readIndexOutOfBoundsException
- when index
is out of boundsbyte read()
DataBuffer read(byte[] destination)
destination
- the array into which the bytes are to be writtenDataBuffer read(byte[] destination, int offset, int length)
length
bytes of this buffer into the specified destination,
starting at the current reading position of this buffer.destination
- the array into which the bytes are to be writtenoffset
- the index within destination
of the first byte to be writtenlength
- the maximum number of bytes to be written in destination
DataBuffer write(byte b)
b
- the byte to be writtenDataBuffer write(byte[] source)
source
- the bytes to be written into this bufferDataBuffer write(byte[] source, int offset, int length)
length
bytes of the given source into this buffer, starting
at the current writing position of this buffer.source
- the bytes to be written into this bufferoffset
- the index within source
to start writing fromlength
- the maximum number of bytes to be written from source
DataBuffer write(DataBuffer... buffers)
DataBuffer
s to this buffer, starting at the current
writing position. It is the responsibility of the caller to
release the given data buffers.buffers
- the byte buffers to write into this bufferDataBuffer write(ByteBuffer... buffers)
ByteBuffer
to this buffer, starting at the current
writing position.buffers
- the byte buffers to write into this bufferdefault DataBuffer write(CharSequence charSequence, Charset charset)
CharSequence
using the given Charset
,
starting at the current writing position.charSequence
- the char sequence to write into this buffercharset
- the charset to encode the char sequence withDataBuffer slice(int index, int length)
DataBuffer
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.
index
- the index at which to start the slicelength
- the length of the slicedefault DataBuffer retainedSlice(int index, int length)
DataBuffer
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 call DataBufferUtils.retain(DataBuffer)
(or equivalent) on the
resulting slice.
index
- the index at which to start the slicelength
- the length of the sliceByteBuffer asByteBuffer()
ByteBuffer
. Data between this
DataBuffer
and the returned ByteBuffer
is shared; though
changes in the returned buffer's position
will not be reflected in the reading nor writing position of this data buffer.ByteBuffer asByteBuffer(int index, int length)
ByteBuffer
. Data between
this DataBuffer
and the returned ByteBuffer
is shared; though
changes in the returned buffer's position
will not be reflected in the reading nor writing position of this data buffer.index
- the index at which to start the byte bufferlength
- the length of the returned byte bufferInputStream asInputStream()
InputStream
. 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.asInputStream(boolean)
InputStream asInputStream(boolean releaseOnClose)
InputStream
. Both data and read position are
shared between the returned stream and this data buffer.OutputStream asOutputStream()
OutputStream
. Both data and write position are
shared between the returned stream and this data buffer.default String toString(Charset charset)
toString(readPosition(), readableByteCount(), charset)
.charset
- the character set to useString toString(int index, int length, Charset charset)
index
- the index at which to start the stringlength
- the number of bytes to use for the stringcharset
- the charset to use