Class DefaultDataBuffer
- All Implemented Interfaces:
DataBuffer
DataBuffer interface that uses a
ByteBuffer internally. with separate read and write positions.
Constructed using the DefaultDataBufferFactory.
Inspired by Netty's ByteBuf. Introduced so that non-Netty runtimes
(i.e. servlet) do not require Netty on the classpath.
- since:
- 5.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Brian Clozel
- see Also:
-
Nested Class summary
Nested classes/interfaces inherited from interface DataBuffer
DataBuffer.ByteProcessor -
Method summary
Modifier and TypeMethodDescriptionDeprecated.asByteBuffer(int index, int length) Deprecated.intcapacity()Return the number of bytes that this buffer can contain.capacity(int capacity) Deprecated.ensureWritable(int length) Ensure that the current buffer has enoughDataBuffer.writableByteCount()to write the amount of data given as an argument.boolean factory()Return theDataBufferFactorythat created this buffer.bytegetByte(int index) Read a single byte at the given index from this data buffer.Directly exposes the nativeByteBufferthat this buffer is based on.inthashCode() 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.slice(int index, int length) Deprecated.split(int index) splits this data buffer into two at the given index.toByteBuffer(int index, int length) Deprecated.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.tostring() Return a part of this buffer's data as 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(ByteBuffer... buffers) Write one or moreByteBufferto this buffer, starting at the current writing position.write(DataBuffer... dataBuffers) Write one or moreDataBuffers to this buffer, 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.Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface DataBuffer
asInputstream, asInputstream, asOutputstream, ensureCapacity, forEachByte, retainedslice, toByteBuffer, toByteBuffer, tostring, write
-
Method Details
-
getNativeBuffer
Directly exposes the nativeByteBufferthat this buffer is based on. The position of the returnedByteBufferis set to the read position, and the Buffer.limit() to the write position.- Returns:
- the wrapped byte buffer
-
factory
Description copied from interface: DataBufferReturn theDataBufferFactorythat created this buffer.- specified by:
factory in interface DataBuffer- Returns:
- the creating buffer factory
-
indexOf
Description copied from interface: DataBufferReturn the index of the first byte in this buffer that matches the given predicate.- specified by:
indexOf in interface DataBuffer- 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
Description copied from interface: DataBufferReturn the index of the last byte in this buffer that matches the given predicate.- specified by:
lastIndexOf in interface DataBuffer- 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
public int readableByteCount()Description copied from interface: DataBufferReturn the number of bytes that can be read from this data buffer.- specified by:
readableByteCount in interface DataBuffer- Returns:
- the readable byte count
-
writableByteCount
public int writableByteCount()Description copied from interface: DataBufferReturn the number of bytes that can be written to this data buffer.- specified by:
writableByteCount in interface DataBuffer- Returns:
- the writable byte count
-
readPosition
public int readPosition()Description copied from interface: DataBufferReturn the position from which this buffer will read.- specified by:
readPosition in interface DataBuffer- Returns:
- the read position
-
readPosition
Description copied from interface: DataBufferset the position from which this buffer will read.- specified by:
readPosition in interface DataBuffer- Parameters:
readPosition- the new read position- Returns:
- this buffer
-
writePosition
public int writePosition()Description copied from interface: DataBufferReturn the position to which this buffer will write.- specified by:
writePosition in interface DataBuffer- Returns:
- the write position
-
writePosition
Description copied from interface: DataBufferset the position to which this buffer will write.- specified by:
writePosition in interface DataBuffer- Parameters:
writePosition- the new write position- Returns:
- this buffer
-
capacity
public int capacity()Description copied from interface: DataBufferReturn the number of bytes that this buffer can contain.- specified by:
capacity in interface DataBuffer- Returns:
- the capacity
-
capacity
Deprecated.Description copied from interface: DataBufferset 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.
- specified by:
capacity in interface DataBuffer- Parameters:
capacity- the new capacity- Returns:
- this buffer
-
ensureWritable
Description copied from interface: DataBufferEnsure that the current buffer has enoughDataBuffer.writableByteCount()to write the amount of data given as an argument. If not, the missing capacity will be added to the buffer.- specified by:
ensureWritable in interface DataBuffer- Parameters:
length- the writable capacity to check for- Returns:
- this buffer
-
getByte
public byte getByte(int index) Description copied from interface: DataBufferRead a single byte at the given index from this data buffer.- specified by:
getByte in interface DataBuffer- Parameters:
index- the index at which the byte will be read- Returns:
- the byte at the given index
-
read
public byte read()Description copied from interface: DataBufferRead a single byte from the current reading position from this data buffer.- specified by:
read in interface DataBuffer- Returns:
- the byte at this buffer's current reading position
-
read
Description copied from interface: DataBufferRead this buffer's data into the specified destination, starting at the current reading position of this buffer.- specified by:
read in interface DataBuffer- Parameters:
destination- the array into which the bytes are to be written- Returns:
- this buffer
-
read
Description copied from interface: DataBufferRead at mostlengthbytes of this buffer into the specified destination, starting at the current reading position of this buffer.- specified by:
read in interface DataBuffer- 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
Description copied from interface: DataBufferWrite a single byte into this buffer at the current writing position.- specified by:
write in interface DataBuffer- Parameters:
b- the byte to be written- Returns:
- this buffer
-
write
Description copied from interface: DataBufferWrite the given source into this buffer, starting at the current writing position of this buffer.- specified by:
write in interface DataBuffer- Parameters:
source- the bytes to be written into this buffer- Returns:
- this buffer
-
write
Description copied from interface: DataBufferWrite at mostlengthbytes of the given source into this buffer, starting at the current writing position of this buffer.- specified by:
write in interface DataBuffer- 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
Description copied from interface: DataBufferWrite 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.- specified by:
write in interface DataBuffer- Parameters:
dataBuffers- the byte buffers to write into this buffer- Returns:
- this buffer
-
write
Description copied from interface: DataBufferWrite one or moreByteBufferto this buffer, starting at the current writing position.- specified by:
write in interface DataBuffer- Parameters:
buffers- the byte buffers to write into this buffer- Returns:
- this buffer
-
slice
Deprecated.Description copied from interface: DataBufferCreate a newDataBufferwhose 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.- specified by:
slice in interface DataBuffer- Parameters:
index- the index at which to start the slicelength- the length of the slice- Returns:
- the specified slice of this data buffer
-
split
Description copied from interface: DataBuffersplits 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, but independent and cannot overlap (unlikeslice).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.- specified by:
split in interface DataBuffer- Parameters:
index- the index at which it should be split- Returns:
- a new data buffer, containing the bytes from index
0toindex
-
asByteBuffer
Deprecated.Description copied from interface: DataBufferExpose this buffer's bytes as aByteBuffer. Data between thisDataBufferand the returnedByteBufferis shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.- specified by:
asByteBuffer in interface DataBuffer- Returns:
- this data buffer as a byte buffer
-
asByteBuffer
Deprecated.Description copied from interface: DataBufferExpose a subsequence of this buffer's bytes as aByteBuffer. Data between thisDataBufferand the returnedByteBufferis shared; though changes in the returned buffer's position will not be reflected in the reading nor writing position of this data buffer.- specified by:
asByteBuffer in interface DataBuffer- 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
-
toByteBuffer
@Deprecated(since="6.0.5") public ByteBuffer toByteBuffer(int index, int length) Deprecated.Description copied from interface: DataBufferReturns aByteBufferrepresentation of a subsequence of this buffer's bytes. Data between thisDataBufferand the returnedByteBufferis not shared.- specified by:
toByteBuffer in interface DataBuffer- Returns:
- this data buffer as a byte buffer
- see Also:
-
toByteBuffer
public void toByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length) Description copied from interface: DataBufferCopies 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.- specified by:
toByteBuffer in interface DataBuffer- 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
-
readableByteBuffers
Description copied from interface: DataBufferReturns a closeable iterator over eachByteBufferin this data buffer that can be read. Calling this method is more efficient thanDataBuffer.toByteBuffer(), as no data is copied. 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.
- specified by:
readableByteBuffers in interface DataBuffer- Returns:
- a closeable iterator over the readable byte buffers contained in this data buffer
-
writableByteBuffers
Description copied from interface: DataBufferReturns 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.
- specified by:
writableByteBuffers in interface DataBuffer- Returns:
- a closeable iterator over the writable byte buffers contained in this data buffer
-
tostring
Description copied from interface: DataBufferReturn a part of this buffer's data as a string using the specified charset.- specified by:
tostring in interface DataBuffer- 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
-
equals
-
hashCode
-
tostring
-