public class DefaultDataBuffer extends java.lang.Object implements DataBuffer
DataBuffer
interface that uses a ByteBuffer
internally, with separate read and write positions. Constructed
using the DefaultDataBufferFactory
.DefaultDataBufferFactory
Modifier and Type | Class and Description |
---|---|
private class |
DefaultDataBuffer.DefaultDataBufferInputStream |
private class |
DefaultDataBuffer.DefaultDataBufferOutputStream |
private static class |
DefaultDataBuffer.SlicedDefaultDataBuffer |
Modifier and Type | Field and Description |
---|---|
private java.nio.ByteBuffer |
byteBuffer |
private DefaultDataBufferFactory |
dataBufferFactory |
private int |
readPosition |
private int |
writePosition |
Constructor and Description |
---|
DefaultDataBuffer(java.nio.ByteBuffer byteBuffer,
DefaultDataBufferFactory dataBufferFactory)
Create a new
DefaultDataBuffer based on the given
ByteBuffer . |
DefaultDataBuffer(java.nio.ByteBuffer byteBuffer,
int readPosition,
int writePosition,
DefaultDataBufferFactory dataBufferFactory) |
Modifier and Type | Method and Description |
---|---|
java.nio.ByteBuffer |
asByteBuffer()
Expose this buffer's bytes as a
ByteBuffer . |
java.io.InputStream |
asInputStream()
Expose this buffer's data as an
InputStream . |
java.io.OutputStream |
asOutputStream()
Expose this buffer's data as an
OutputStream . |
private void |
ensureExtraCapacity(int extraCapacity) |
boolean |
equals(java.lang.Object obj) |
DefaultDataBufferFactory |
factory()
Return the
DataBufferFactory that created this buffer. |
java.nio.ByteBuffer |
getNativeBuffer()
Directly exposes the native
ByteBuffer that this buffer is based on. |
(package private) void |
grow(int minCapacity) |
int |
hashCode() |
int |
indexOf(java.util.function.IntPredicate predicate,
int fromIndex)
Return the index of the first byte in this buffer that matches the given
predicate.
|
int |
lastIndexOf(java.util.function.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 of this data buffer.
|
DefaultDataBuffer |
read(byte[] destination)
Read this buffer's data into the specified destination, starting at the current
reading position of this buffer.
|
DefaultDataBuffer |
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.
|
private <T> T |
readInternal(java.util.function.Function<java.nio.ByteBuffer,T> function)
Internal read method that keeps track of the
readPosition before and after
applying the given function on byteBuffer . |
DataBuffer |
slice(int index,
int length)
Create a new
DataBuffer whose contents is a shared subsequence of this
data buffer's content. |
java.lang.String |
toString() |
DefaultDataBuffer |
write(byte b)
Write a single byte into this buffer at the current writing position.
|
DefaultDataBuffer |
write(byte[] source)
Write the given source into this buffer, startin at the current writing position
of this buffer.
|
DefaultDataBuffer |
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. |
DefaultDataBuffer |
write(java.nio.ByteBuffer... byteBuffers)
Write one or more
ByteBuffer to this buffer, 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. |
private <T> T |
writeInternal(java.util.function.Function<java.nio.ByteBuffer,T> function)
Internal write method that keeps track of the
writePosition before and
after applying the given function on byteBuffer . |
private final DefaultDataBufferFactory dataBufferFactory
private java.nio.ByteBuffer byteBuffer
private int readPosition
private int writePosition
DefaultDataBuffer(java.nio.ByteBuffer byteBuffer, DefaultDataBufferFactory dataBufferFactory)
DefaultDataBuffer
based on the given
ByteBuffer
. Both reading and writing position of this buffer are
based on the current position of the given buffer.byteBuffer
- the buffer to base this buffer onDefaultDataBuffer(java.nio.ByteBuffer byteBuffer, int readPosition, int writePosition, DefaultDataBufferFactory dataBufferFactory)
public java.nio.ByteBuffer getNativeBuffer()
ByteBuffer
that this buffer is based on.public DefaultDataBufferFactory factory()
DataBuffer
DataBufferFactory
that created this buffer.factory
in interface DataBuffer
public int indexOf(java.util.function.IntPredicate predicate, int fromIndex)
DataBuffer
indexOf
in interface DataBuffer
predicate
- the predicate to matchfromIndex
- the index to start the search frompredicate
; or -1
if none matchpublic int lastIndexOf(java.util.function.IntPredicate predicate, int fromIndex)
DataBuffer
lastIndexOf
in interface DataBuffer
predicate
- the predicate to matchfromIndex
- the index to start the search frompredicate
; or -1
if none matchpublic int readableByteCount()
DataBuffer
readableByteCount
in interface DataBuffer
public byte read()
DataBuffer
read
in interface DataBuffer
public DefaultDataBuffer read(byte[] destination)
DataBuffer
read
in interface DataBuffer
destination
- the array into which the bytes are to be writtenpublic DefaultDataBuffer read(byte[] destination, int offset, int length)
DataBuffer
length
bytes of this buffer into the specified destination,
starting at the current reading position of this buffer.read
in interface DataBuffer
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
private <T> T readInternal(java.util.function.Function<java.nio.ByteBuffer,T> function)
readPosition
before and after
applying the given function on byteBuffer
.public DefaultDataBuffer write(byte b)
DataBuffer
write
in interface DataBuffer
b
- the byte to be writtenpublic DefaultDataBuffer write(byte[] source)
DataBuffer
write
in interface DataBuffer
source
- the bytes to be written into this bufferpublic DefaultDataBuffer write(byte[] source, int offset, int length)
DataBuffer
length
bytes of the given source into this buffer, starting
at the current writing position of this buffer.write
in interface DataBuffer
source
- the bytes to be written into this bufferoffset
- the index withing source
to start writing fromlength
- the maximum number of bytes to be written from source
public DataBuffer write(DataBuffer... buffers)
DataBuffer
DataBuffer
s to this buffer, starting at the current
writing position.write
in interface DataBuffer
buffers
- the byte buffers to write into this bufferpublic DefaultDataBuffer write(java.nio.ByteBuffer... byteBuffers)
DataBuffer
ByteBuffer
to this buffer, starting at the current
writing position.write
in interface DataBuffer
byteBuffers
- the byte buffers to write into this bufferprivate <T> T writeInternal(java.util.function.Function<java.nio.ByteBuffer,T> function)
writePosition
before and
after applying the given function on byteBuffer
.public DataBuffer slice(int index, int length)
DataBuffer
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.slice
in interface DataBuffer
index
- the index at which to start the slicelength
- the length of the slicepublic java.nio.ByteBuffer asByteBuffer()
DataBuffer
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.asByteBuffer
in interface DataBuffer
public java.io.InputStream asInputStream()
DataBuffer
InputStream
. Both data and position are
shared between the returned stream and this data buffer.asInputStream
in interface DataBuffer
public java.io.OutputStream asOutputStream()
DataBuffer
OutputStream
. Both data and position are
shared between the returned stream and this data buffer.asOutputStream
in interface DataBuffer
private void ensureExtraCapacity(int extraCapacity)
void grow(int minCapacity)
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object