- All Known Subinterfaces:
CloseableDataBuffer,PooledDataBuffer,TouchableDataBuffer
- All Known Implementing Classes:
DataBufferWrapper,DefaultDataBuffer
DataBuffers 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 io.netty.buffer.ByteBuf but can
also be used on non-Netty platforms (i.e. Servlet containers).
- Since:
- 5.0
- Author:
- Arjen Poutsma, Brian Clozel
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA dedicated iterator type that ensures the lifecycle of iteratedByteBufferelements. -
Method Summary
Modifier and TypeMethodDescriptiondefault InputStreamExpose this buffer's data as anInputStream.default InputStreamasInputStream(boolean releaseOnClose) Expose this buffer's data as anInputStream.default OutputStreamExpose this buffer's data as anOutputStream.default WriterasWriter()Expose this buffer's data as anWriter.default WriterExpose this buffer's data as anWriter.intcapacity()Return the number of bytes that this buffer can contain.clear()Clears this buffer.ensureWritable(int capacity) Ensure that the current buffer has enoughwritableByteCount()to write the amount of data given as an argument.factory()Return theDataBufferFactorythat created this buffer.bytegetByte(int index) Read a single byte at the given index from this data buffer.intindexOf(IntPredicate predicate, int fromIndex) Return the index of the first byte in this buffer that matches the given predicate.intlastIndexOf(IntPredicate predicate, int fromIndex) Return the index of the last byte in this buffer that matches the given predicate.byteread()Read a single byte from the current reading position from this data buffer.read(byte[] destination) Read this buffer's data into the specified destination, starting at the current reading position of this buffer.read(byte[] destination, int offset, int length) Read at mostlengthbytes of this buffer into the specified destination, starting at the current reading position of this buffer.Returns a closeable iterator over eachByteBufferin this data buffer that can be read.intReturn the number of bytes that can be read from this data buffer.intReturn the position from which this buffer will read.readPosition(int readPosition) Set the position from which this buffer will read.split(int index) Splits this data buffer into two at the given index.voidtoByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length) Copies the given length from this data buffer into the given destinationByteBuffer, beginning at the given source position, and the given destination position in the destination byte buffer.default voidtoByteBuffer(ByteBuffer dest) Copies this entire data buffer into the given destinationByteBuffer, beginning at the current reading position, and the current position of destination byte buffer.Return a part of this buffer's data as a String using the specified charset.default StringReturn this buffer's data a String using the specified charset.Returns a closeable iterator over eachByteBufferin this data buffer that can be written to.intReturn the number of bytes that can be written to this data buffer.write(byte b) Write a single byte into this buffer at the current writing position.write(byte[] source) Write the given source into this buffer, starting at the current writing position of this buffer.write(byte[] source, int offset, int length) Write at mostlengthbytes of the given source into this buffer, starting at the current writing position of this buffer.write(DataBuffer... buffers) Write one or moreDataBuffers to this buffer, starting at the current writing position.default DataBufferwrite(CharSequence charSequence, Charset charset) Write the givenCharSequenceusing the givenCharset, starting at the current writing position.write(ByteBuffer... buffers) Write one or moreByteBufferto this buffer, starting at the current writing position.default DataBufferwrite(CharBuffer src, Charset charset) Write the givenCharBufferusing the givenCharset, starting at the current writing position.intReturn the position to which this buffer will write.writePosition(int writePosition) Set the position to which this buffer will write.
-
Method Details
-
factory
DataBufferFactory factory()Return theDataBufferFactorythat created this buffer.- Returns:
- the creating buffer factory
-
indexOf
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-1if none match
-
lastIndexOf
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-1if 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
-
duplicate
DataBuffer duplicate() -
ensureWritable
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:
- 6.0
-
readPosition
int readPosition()Return the position from which this buffer will read.- Returns:
- the read position
- Since:
- 5.0.1
-
readPosition
Set the position from which this buffer will read.- Parameters:
readPosition- the new read position- Returns:
- this buffer
- Throws:
IndexOutOfBoundsException- ifreadPositionis 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
Set the position to which this buffer will write.- Parameters:
writePosition- the new write position- Returns:
- this buffer
- Throws:
IndexOutOfBoundsException- ifwritePositionis 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- whenindexis 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
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
Read at mostlengthbytes 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 withindestinationof the first byte to be writtenlength- the maximum number of bytes to be written indestination- Returns:
- this buffer
-
write
Write a single byte into this buffer at the current writing position.- Parameters:
b- the byte to be written- Returns:
- this buffer
-
write
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
Write at mostlengthbytes 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 withinsourceto start writing fromlength- the maximum number of bytes to be written fromsource- Returns:
- this buffer
-
write
Write one or moreDataBuffers 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
Write one or moreByteBufferto this buffer, starting at the current writing position.- Parameters:
buffers- the byte buffers to write into this buffer- Returns:
- this buffer
-
write
Write the givenCharSequenceusing 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
-
write
Write the givenCharBufferusing the givenCharset, starting at the current writing position.- Parameters:
src- the char sequence to write into this buffercharset- the charset to encode the char sequence with- Returns:
- this buffer
- Since:
- 5.1.4
-
split
Splits this data buffer into two at the given index.Data that precedes the
indexwill be returned in a new buffer, while this buffer will contain data that follows afterindex. Memory between the two buffers is shared.The read and write position of the returned buffer are truncated to fit within the buffers capacity if necessary. The positions of this buffer are set to
0if they are smaller thanindex.- Parameters:
index- the index at which it should be split- Returns:
- a new data buffer, containing the bytes from index
0toindex - Since:
- 6.0
-
toByteBuffer
Copies this entire data buffer into the given destinationByteBuffer, beginning at the current reading position, and the current position of destination byte buffer.- Parameters:
dest- the destination byte buffer- Since:
- 6.0.5
-
toByteBuffer
Copies the given length from this data buffer into the given destinationByteBuffer, beginning at the given source position, and the given destination position in the destination byte buffer.- Parameters:
srcPos- the position of this data buffer from where copying should startdest- the destination byte bufferdestPos- the position indestto where copying should startlength- the amount of data to copy- Since:
- 6.0.5
-
readableByteBuffers
DataBuffer.ByteBufferIterator readableByteBuffers()Returns a closeable iterator over eachByteBufferin this data buffer that can be read. However, the byte buffers provided can only be used during the iteration.Note that the returned iterator must be used in a try-with-resources clause or explicitly closed.
- Returns:
- a closeable iterator over the readable byte buffers contained in this data buffer
- Since:
- 6.0.5
-
writableByteBuffers
DataBuffer.ByteBufferIterator writableByteBuffers()Returns a closeable iterator over eachByteBufferin this data buffer that can be written to. The byte buffers provided can only be used during the iteration.Note that the returned iterator must be used in a try-with-resources clause or explicitly closed.
- Returns:
- a closeable iterator over the writable byte buffers contained in this data buffer
- Since:
- 6.0.5
-
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
Expose this buffer's data as anInputStream. Both data and read position are shared between the returned stream and this data buffer. -
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
-
asWriter
Expose this buffer's data as anWriter. Both data and write position are shared between the returned stream and this data buffer. UsesUTF-8charset.- Returns:
- this data buffer as an output stream
-
asWriter
Expose this buffer's data as anWriter. Both data and write position are shared between the returned stream and this data buffer.- Parameters:
charset- Charset to use.- Returns:
- this data buffer as an output stream
-
toString
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
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
-
clear
DataBuffer clear()Clears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.
- Returns:
- This buffer
-