Class NettyDataBuffer
java.lang.Object
org.springframework.core.io.buffer.NettyDataBuffer
- All Implemented Interfaces:
DataBuffer,PooledDataBuffer
Implementation of the
DataBuffer interface that wraps a Netty
ByteBuf. Typically constructed with NettyDataBufferFactory.- Since:
- 5.0
- Author:
- Arjen Poutsma, Brian Clozel
-
Method Summary
Modifier and TypeMethodDescriptionExpose this buffer's bytes as aByteBuffer.asByteBuffer(int index, int length) Expose a subsequence of this buffer's bytes as aByteBuffer.Expose this buffer's data as anInputStream.asInputStream(boolean releaseOnClose) Expose this buffer's data as anInputStream.Expose this buffer's data as anOutputStream.intcapacity()Return the number of bytes that this buffer can contain.capacity(int capacity) Set the number of bytes that this buffer can contain.ensureCapacity(int capacity) Ensure that the current buffer has enoughDataBuffer.writableByteCount()to write the amount of data given as an argument.booleanfactory()Return theDataBufferFactorythat created this buffer.bytegetByte(int index) Read a single byte at the given index from this data buffer.io.netty.buffer.ByteBufDirectly exposes the nativeByteBufthat 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.booleanReturntrueif this buffer is allocated;falseif it has been deallocated.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.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.booleanrelease()Decrease the reference count for this buffer by one, and deallocate it once the count reaches zero.retain()Increase the reference count for this buffer by one.retainedSlice(int index, int length) Create a newDataBufferwhose contents is a shared, retained subsequence of this data buffer's content.slice(int index, int length) Create a newDataBufferwhose contents is a shared subsequence of this data buffer's content.toString()Return a part of this buffer's data as a String using the specified charset.Return this buffer's data a String using the specified charset.Associate the given hint with the data buffer for debugging purposes.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(io.netty.buffer.ByteBuf... byteBufs) Writes one or more NettyByteBufsto this buffer, starting at the current writing position.write(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.write(DataBuffer... buffers) 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.
-
Method Details
-
getNativeBuffer
public io.netty.buffer.ByteBuf getNativeBuffer()Directly exposes the nativeByteBufthat this buffer is based on.- Returns:
- the wrapped byte buffer
-
factory
Description copied from interface:DataBufferReturn theDataBufferFactorythat created this buffer.- Specified by:
factoryin interfaceDataBuffer- 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:
indexOfin interfaceDataBuffer- 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:
lastIndexOfin interfaceDataBuffer- 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:
readableByteCountin interfaceDataBuffer- 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:
writableByteCountin interfaceDataBuffer- Returns:
- the writable byte count
-
readPosition
public int readPosition()Description copied from interface:DataBufferReturn the position from which this buffer will read.- Specified by:
readPositionin interfaceDataBuffer- Returns:
- the read position
-
readPosition
Description copied from interface:DataBufferSet the position from which this buffer will read.- Specified by:
readPositionin interfaceDataBuffer- 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:
writePositionin interfaceDataBuffer- Returns:
- the write position
-
writePosition
Description copied from interface:DataBufferSet the position to which this buffer will write.- Specified by:
writePositionin interfaceDataBuffer- Parameters:
writePosition- the new write position- 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:
getBytein interfaceDataBuffer- Parameters:
index- the index at which the byte will be read- Returns:
- the byte at the given index
-
capacity
public int capacity()Description copied from interface:DataBufferReturn the number of bytes that this buffer can contain.- Specified by:
capacityin interfaceDataBuffer- Returns:
- the capacity
-
capacity
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:
capacityin interfaceDataBuffer- Parameters:
capacity- the new capacity- Returns:
- this buffer
-
ensureCapacity
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:
ensureCapacityin interfaceDataBuffer- Parameters:
capacity- the writable capacity to check for- Returns:
- this buffer
-
read
public byte read()Description copied from interface:DataBufferRead a single byte from the current reading position from this data buffer.- Specified by:
readin interfaceDataBuffer- 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:
readin interfaceDataBuffer- 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:
readin interfaceDataBuffer- 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:
writein interfaceDataBuffer- 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:
writein interfaceDataBuffer- 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:
writein interfaceDataBuffer- 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:
writein interfaceDataBuffer- Parameters:
buffers- 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:
writein interfaceDataBuffer- Parameters:
buffers- the byte buffers to write into this buffer- Returns:
- this buffer
-
write
Writes one or more NettyByteBufsto this buffer, starting at the current writing position.- Parameters:
byteBufs- the buffers to write into this buffer- Returns:
- this buffer
-
write
Description copied from interface:DataBufferWrite the givenCharSequenceusing the givenCharset, starting at the current writing position.- Specified by:
writein interfaceDataBuffer- Parameters:
charSequence- the char sequence to write into this buffercharset- the charset to encode the char sequence with- Returns:
- this buffer
-
slice
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:
slicein interfaceDataBuffer- Parameters:
index- the index at which to start the slicelength- the length of the slice- Returns:
- the specified slice of this data buffer
-
retainedSlice
Description copied from interface:DataBufferCreate a newDataBufferwhose 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
DataBuffer.slice(int, int), this method will callDataBufferUtils.retain(DataBuffer)(or equivalent) on the resulting slice.- Specified by:
retainedSlicein interfaceDataBuffer- Parameters:
index- the index at which to start the slicelength- the length of the slice- Returns:
- the specified, retained slice of this data buffer
-
asByteBuffer
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:
asByteBufferin interfaceDataBuffer- Returns:
- this data buffer as a byte buffer
-
asByteBuffer
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:
asByteBufferin interfaceDataBuffer- 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
-
asInputStream
Description copied from interface:DataBufferExpose 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.- Specified by:
asInputStreamin interfaceDataBuffer- Returns:
- this data buffer as an input stream
- See Also:
-
asInputStream
Description copied from interface:DataBufferExpose this buffer's data as anInputStream. Both data and read position are shared between the returned stream and this data buffer.- Specified by:
asInputStreamin interfaceDataBuffer- Parameters:
releaseOnClose- whether the underlying buffer will be released when the input stream is closed.- Returns:
- this data buffer as an input stream
-
asOutputStream
Description copied from interface:DataBufferExpose this buffer's data as anOutputStream. Both data and write position are shared between the returned stream and this data buffer.- Specified by:
asOutputStreamin interfaceDataBuffer- Returns:
- this data buffer as an output stream
-
toString
Description copied from interface:DataBufferReturn this buffer's data a String using the specified charset. Default implementation delegates totoString(readPosition(), readableByteCount(), charset).- Specified by:
toStringin interfaceDataBuffer- Parameters:
charset- the character set to use- Returns:
- a string representation of all this buffers data
-
toString
Description copied from interface:DataBufferReturn a part of this buffer's data as a String using the specified charset.- Specified by:
toStringin interfaceDataBuffer- 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
-
isAllocated
public boolean isAllocated()Description copied from interface:PooledDataBufferReturntrueif this buffer is allocated;falseif it has been deallocated.- Specified by:
isAllocatedin interfacePooledDataBuffer
-
retain
Description copied from interface:PooledDataBufferIncrease the reference count for this buffer by one.- Specified by:
retainin interfacePooledDataBuffer- Returns:
- this buffer
-
touch
Description copied from interface:PooledDataBufferAssociate the given hint with the data buffer for debugging purposes.- Specified by:
touchin interfacePooledDataBuffer- Returns:
- this buffer
-
release
public boolean release()Description copied from interface:PooledDataBufferDecrease the reference count for this buffer by one, and deallocate it once the count reaches zero.- Specified by:
releasein interfacePooledDataBuffer- Returns:
trueif the buffer was deallocated;falseotherwise
-
equals
-
hashCode
public int hashCode() -
toString
-