Top Description Inners Methods
org.springframework.core.io.buffer

public Interface DataBuffer

Known Direct Subinterfaces
org.springframework.core.io.buffer.TouchableDataBuffer, org.springframework.core.io.buffer.CloseableDataBuffer
Known Direct Implementers
org.springframework.core.io.buffer.DataBufferWrapper, org.springframework.core.io.buffer.DefaultDataBuffer
Imports
java.io.Closeable, .InputStream, .OutputStream, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.Charset, .CharsetEncoder, .CoderResult, .CodingErrorAction, java.util.Iterator, java.util.function.IntPredicate, org.springframework.util.Assert

Basic abstraction over byte buffers.

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).

Authors
Arjen Poutsma, Brian Clozel
Since
5.0
See Also
DataBufferFactory

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
DataBuffer.ByteBufferIterator

A dedicated iterator type that ensures the lifecycle of iterated ByteBuffer elements.

Method Summary

Modifier and TypeMethod and Description
public ByteBuffer

Returns:

this data buffer as a byte buffer
asByteBuffer
()

Deprecated since 6.0. as of 6.0, in favor of toByteBuffer(ByteBuffer), readableByteBuffers(), or writableByteBuffers().
Expose this buffer's bytes as a ByteBuffer.
public ByteBuffer

Returns:

this data buffer as a byte buffer
asByteBuffer
(int
the index at which to start the byte buffer
index
,
int
the length of the returned byte buffer
length
)

Deprecated since 6.0. as of 6.0, in favor of toByteBuffer(int, ByteBuffer, int, int), readableByteBuffers(), or writableByteBuffers().
Expose a subsequence of this buffer's bytes as a ByteBuffer.
public default InputStream

Returns:

this data buffer as an input stream
asInputStream
()

Expose this buffer's data as an InputStream.

public default InputStream

Returns:

this data buffer as an input stream
asInputStream
(boolean
whether the underlying buffer will be released when the input stream is closed.
releaseOnClose
)

Expose this buffer's data as an InputStream.

public default OutputStream

Returns:

this data buffer as an output stream
asOutputStream
()

Expose this buffer's data as an OutputStream.

public int

Returns:

the capacity
capacity
()

Return the number of bytes that this buffer can contain.

public DataBuffer

Returns:

this buffer
capacity
(int
the new capacity
capacity
)

Deprecated since 6.0. as of 6.0, in favor of ensureWritable(int), which has different semantics
Set the number of bytes that this buffer can contain.
public default DataBuffer

Returns:

this buffer
ensureCapacity
(int
the writable capacity to check for
capacity
)

Deprecated since 6.0. since 6.0, in favor of ensureWritable(int)
Ensure that the current buffer has enough writableByteCount() to write the amount of data given as an argument.
public DataBuffer

Returns:

this buffer
ensureWritable
(int
the writable capacity to check for
capacity
)

Ensure that the current buffer has enough writableByteCount() to write the amount of data given as an argument.

public DataBufferFactory

Returns:

the creating buffer factory
factory
()

Return the DataBufferFactory that created this buffer.

public byte

Returns:

the byte at the given index
getByte
(int
the index at which the byte will be read
index
)

Read a single byte at the given index from this data buffer.

public int

Returns:

the index of the first byte that matches predicate; or -1 if none match
indexOf
(IntPredicate
the predicate to match
predicate
,
int
the index to start the search from
fromIndex
)

Return the index of the first byte in this buffer that matches the given predicate.

public int

Returns:

the index of the last byte that matches predicate; or -1 if none match
lastIndexOf
(IntPredicate
the predicate to match
predicate
,
int
the index to start the search from
fromIndex
)

Return the index of the last byte in this buffer that matches the given predicate.

public byte

Returns:

the byte at this buffer's current reading position
read
()

Read a single byte from the current reading position from this data buffer.

public DataBuffer

Returns:

this buffer
read
(byte[]
the array into which the bytes are to be written
destination
)

Read this buffer's data into the specified destination, starting at the current reading position of this buffer.

public DataBuffer

Returns:

this buffer
read
(byte[]
the array into which the bytes are to be written
destination
,
int
the index within destination of the first byte to be written
offset
,
int
the maximum number of bytes to be written in destination
length
)

Read at most length bytes of this buffer into the specified destination, starting at the current reading position of this buffer.

public DataBuffer.ByteBufferIterator

Returns:

a closeable iterator over the readable byte buffers contained in this data buffer
readableByteBuffers
()

Returns a closeable iterator over each ByteBuffer in this data buffer that can be read.

public int

Returns:

the readable byte count
readableByteCount
()

Return the number of bytes that can be read from this data buffer.

public int

Returns:

the read position
readPosition
()

Return the position from which this buffer will read.

public DataBuffer

Returns:

this buffer
readPosition
(int
the new read position
readPosition
)

Set the position from which this buffer will read.

public default DataBuffer

Returns:

the specified, retained slice of this data buffer
retainedSlice
(int
the index at which to start the slice
index
,
int
the length of the slice
length
)

Deprecated since 6.0. as of 6.0, in favor of split(int), which has different semantics
Create a new DataBuffer whose contents is a shared, retained subsequence of this data buffer's content.
public DataBuffer

Returns:

the specified slice of this data buffer
slice
(int
the index at which to start the slice
index
,
int
the length of the slice
length
)

Deprecated since 6.0. as of 6.0, in favor of split(int), which has different semantics
Create a new DataBuffer whose contents is a shared subsequence of this data buffer's content.
public DataBuffer

Returns:

a new data buffer, containing the bytes from index 0 to index
split
(int
the index at which it should be split
index
)

Splits this data buffer into two at the given index.

public default ByteBuffer

Returns:

this data buffer as a byte buffer
toByteBuffer
()

Deprecated since 6.0.5. as of 6.0.5, in favor of toByteBuffer(ByteBuffer)
Returns a ByteBuffer representation of this data buffer.
public ByteBuffer

Returns:

this data buffer as a byte buffer
toByteBuffer
(int index, int length)

Deprecated since 6.0.5. as of 6.0.5, in favor of toByteBuffer(int, ByteBuffer, int, int)
Returns a ByteBuffer representation of a subsequence of this buffer's bytes.
public default void
toByteBuffer(ByteBuffer
the destination byte buffer
dest
)

Copies this entire data buffer into the given destination ByteBuffer, beginning at the current reading position, and the current position of destination byte buffer.

public void
toByteBuffer(int
the position of this data buffer from where copying should start
srcPos
,
ByteBuffer
the destination byte buffer
dest
,
int
the position in dest to where copying should start
destPos
,
int
the amount of data to copy
length
)

Copies the given length from this data buffer into the given destination ByteBuffer, beginning at the given source position, and the given destination position in the destination byte buffer.

public default String

Returns:

a string representation of all this buffers data
toString
(Charset
the character set to use
charset
)

Return this buffer's data a String using the specified charset.

public String

Returns:

a string representation of a part of this buffers data
toString
(int
the index at which to start the string
index
,
int
the number of bytes to use for the string
length
,
Charset
the charset to use
charset
)

Return a part of this buffer's data as a String using the specified charset.

public DataBuffer.ByteBufferIterator

Returns:

a closeable iterator over the writable byte buffers contained in this data buffer
writableByteBuffers
()

Returns a closeable iterator over each ByteBuffer in this data buffer that can be written to.

public int

Returns:

the writable byte count
writableByteCount
()

Return the number of bytes that can be written to this data buffer.

public DataBuffer

Returns:

this buffer
write
(byte
the byte to be written
b
)

Write a single byte into this buffer at the current writing position.

public DataBuffer

Returns:

this buffer
write
(byte[]
the bytes to be written into this buffer
source
)

Write the given source into this buffer, starting at the current writing position of this buffer.

public DataBuffer

Returns:

this buffer
write
(byte[]
the bytes to be written into this buffer
source
,
int
the index within source to start writing from
offset
,
int
the maximum number of bytes to be written from source
length
)

Write at most length bytes of the given source into this buffer, starting at the current writing position of this buffer.

public DataBuffer

Returns:

this buffer
write
(DataBuffer...
the byte buffers to write into this buffer
buffers
)

Write one or more DataBuffers to this buffer, starting at the current writing position.

public DataBuffer

Returns:

this buffer
write
(ByteBuffer...
the byte buffers to write into this buffer
buffers
)

Write one or more ByteBuffer to this buffer, starting at the current writing position.

public default DataBuffer

Returns:

this buffer
write
(CharSequence
the char sequence to write into this buffer
charSequence
,
Charset
the charset to encode the char sequence with
charset
)

Write the given CharSequence using the given Charset, starting at the current writing position.

public int

Returns:

the write position
writePosition
()

Return the position to which this buffer will write.

public DataBuffer

Returns:

this buffer
writePosition
(int
the new write position
writePosition
)

Set the position to which this buffer will write.

Method Detail

asByteBufferback to summary
public ByteBuffer asByteBuffer()

Deprecated

since 6.0.

as of 6.0, in favor of toByteBuffer(ByteBuffer), readableByteBuffers(), or writableByteBuffers().

Expose this buffer's bytes as a 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.

Returns:ByteBuffer

this data buffer as a byte buffer

Annotations
@Deprecated
since:6.0
asByteBufferback to summary
public ByteBuffer asByteBuffer(int index, int length)

Expose a subsequence of this buffer's bytes as a 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.

Parameters
index:int

the index at which to start the byte buffer

length:int

the length of the returned byte buffer

Returns:ByteBuffer

this data buffer as a byte buffer

Annotations
@Deprecated
since:6.0
Since
5.0.1
asInputStreamback to summary
public default InputStream asInputStream()

Expose this buffer's data as an 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.

Returns:InputStream

this data buffer as an input stream

See Also
asInputStream(boolean)
asInputStreamback to summary
public default InputStream asInputStream(boolean releaseOnClose)

Expose this buffer's data as an InputStream. Both data and read position are shared between the returned stream and this data buffer.

Parameters
releaseOnClose:boolean

whether the underlying buffer will be released when the input stream is closed.

Returns:InputStream

this data buffer as an input stream

Since
5.0.4
asOutputStreamback to summary
public default OutputStream asOutputStream()

Expose this buffer's data as an OutputStream. Both data and write position are shared between the returned stream and this data buffer.

Returns:OutputStream

this data buffer as an output stream

capacityback to summary
public int capacity()

Return the number of bytes that this buffer can contain.

Returns:int

the capacity

Since
5.0.1
capacityback to summary
public DataBuffer capacity(int capacity)

Deprecated

since 6.0.

as of 6.0, in favor of ensureWritable(int), which has different semantics

Set 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.

Parameters
capacity:int

the new capacity

Returns:DataBuffer

this buffer

Annotations
@Deprecated
since:6.0
ensureCapacityback to summary
public default DataBuffer ensureCapacity(int capacity)

Deprecated

since 6.0.

since 6.0, in favor of ensureWritable(int)

Ensure that the current buffer has enough writableByteCount() to write the amount of data given as an argument. If not, the missing capacity will be added to the buffer.

Parameters
capacity:int

the writable capacity to check for

Returns:DataBuffer

this buffer

Annotations
@Deprecated
since:6.0
Since
5.1.4
ensureWritableback to summary
public DataBuffer ensureWritable(int capacity)

Ensure that the current buffer has enough writableByteCount() to write the amount of data given as an argument. If not, the missing capacity will be added to the buffer.

Parameters
capacity:int

the writable capacity to check for

Returns:DataBuffer

this buffer

Since
6.0
factoryback to summary
public DataBufferFactory factory()

Return the DataBufferFactory that created this buffer.

Returns:DataBufferFactory

the creating buffer factory

getByteback to summary
public byte getByte(int index)

Read a single byte at the given index from this data buffer.

Parameters
index:int

the index at which the byte will be read

Returns:byte

the byte at the given index

Exceptions
IndexOutOfBoundsException:
when index is out of bounds
Since
5.0.4
indexOfback to summary
public int indexOf(IntPredicate predicate, int fromIndex)

Return the index of the first byte in this buffer that matches the given predicate.

Parameters
predicate:IntPredicate

the predicate to match

fromIndex:int

the index to start the search from

Returns:int

the index of the first byte that matches predicate; or -1 if none match

lastIndexOfback to summary
public int lastIndexOf(IntPredicate predicate, int fromIndex)

Return the index of the last byte in this buffer that matches the given predicate.

Parameters
predicate:IntPredicate

the predicate to match

fromIndex:int

the index to start the search from

Returns:int

the index of the last byte that matches predicate; or -1 if none match

readback to summary
public byte read()

Read a single byte from the current reading position from this data buffer.

Returns:byte

the byte at this buffer's current reading position

readback to summary
public DataBuffer read(byte[] destination)

Read this buffer's data into the specified destination, starting at the current reading position of this buffer.

Parameters
destination:byte[]

the array into which the bytes are to be written

Returns:DataBuffer

this buffer

readback to summary
public 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.

Parameters
destination:byte[]

the array into which the bytes are to be written

offset:int

the index within destination of the first byte to be written

length:int

the maximum number of bytes to be written in destination

Returns:DataBuffer

this buffer

readableByteBuffersback to summary
public DataBuffer.ByteBufferIterator readableByteBuffers()

Returns a closeable iterator over each ByteBuffer in this data buffer that can be read. Calling this method is more efficient than 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.

Returns:DataBuffer.ByteBufferIterator

a closeable iterator over the readable byte buffers contained in this data buffer

Since
6.0.5
readableByteCountback to summary
public int readableByteCount()

Return the number of bytes that can be read from this data buffer.

Returns:int

the readable byte count

readPositionback to summary
public int readPosition()

Return the position from which this buffer will read.

Returns:int

the read position

Since
5.0.1
readPositionback to summary
public DataBuffer readPosition(int readPosition)

Set the position from which this buffer will read.

Parameters
readPosition:int

the new read position

Returns:DataBuffer

this buffer

Exceptions
IndexOutOfBoundsException:
if readPosition is smaller than 0 or greater than writePosition()
Since
5.0.1
retainedSliceback to summary
public default DataBuffer retainedSlice(int index, int length)

Deprecated

since 6.0.

as of 6.0, in favor of split(int), which has different semantics

Create a new 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.

Parameters
index:int

the index at which to start the slice

length:int

the length of the slice

Returns:DataBuffer

the specified, retained slice of this data buffer

Annotations
@Deprecated
since:6.0
Since
5.2
sliceback to summary
public DataBuffer slice(int index, int length)

Deprecated

since 6.0.

as of 6.0, in favor of split(int), which has different semantics

Create a new 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.

Parameters
index:int

the index at which to start the slice

length:int

the length of the slice

Returns:DataBuffer

the specified slice of this data buffer

Annotations
@Deprecated
since:6.0
splitback to summary
public DataBuffer split(int index)

Splits this data buffer into two at the given index.

Data that precedes the index will be returned in a new buffer, while this buffer will contain data that follows after index. Memory between the two buffers is shared, but independent and cannot overlap (unlike slice).

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 0 if they are smaller than index.

Parameters
index:int

the index at which it should be split

Returns:DataBuffer

a new data buffer, containing the bytes from index 0 to index

Since
6.0
toByteBufferback to summary
public default ByteBuffer toByteBuffer()

Deprecated

since 6.0.5.

as of 6.0.5, in favor of toByteBuffer(ByteBuffer)

Returns a ByteBuffer representation of this data buffer. Data between this DataBuffer and the returned ByteBuffer is not shared.

Returns:ByteBuffer

this data buffer as a byte buffer

Annotations
@Deprecated
since:6.0.5
Since
6.0
See Also
readableByteBuffers(), writableByteBuffers()
toByteBufferback to summary
public ByteBuffer toByteBuffer(int index, int length)

Deprecated

since 6.0.5.

as of 6.0.5, in favor of toByteBuffer(int, ByteBuffer, int, int)

Returns a ByteBuffer representation of a subsequence of this buffer's bytes. Data between this DataBuffer and the returned ByteBuffer is not shared.

Returns:ByteBuffer

this data buffer as a byte buffer

Annotations
@Deprecated
since:6.0.5
Since
6.0
See Also
readableByteBuffers(), writableByteBuffers()
toByteBufferback to summary
public default void toByteBuffer(ByteBuffer dest)

Copies this entire data buffer into the given destination ByteBuffer, beginning at the current reading position, and the current position of destination byte buffer.

Parameters
dest:ByteBuffer

the destination byte buffer

Since
6.0.5
toByteBufferback to summary
public void toByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length)

Copies the given length from this data buffer into the given destination ByteBuffer, beginning at the given source position, and the given destination position in the destination byte buffer.

Parameters
srcPos:int

the position of this data buffer from where copying should start

dest:ByteBuffer

the destination byte buffer

destPos:int

the position in dest to where copying should start

length:int

the amount of data to copy

Since
6.0.5
toStringback to summary
public default String toString(Charset charset)

Return this buffer's data a String using the specified charset. Default implementation delegates to toString(readPosition(), readableByteCount(), charset).

Parameters
charset:Charset

the character set to use

Returns:String

a string representation of all this buffers data

Since
5.2
toStringback to summary
public String toString(int index, int length, Charset charset)

Return a part of this buffer's data as a String using the specified charset.

Parameters
index:int

the index at which to start the string

length:int

the number of bytes to use for the string

charset:Charset

the charset to use

Returns:String

a string representation of a part of this buffers data

Since
5.2
writableByteBuffersback to summary
public DataBuffer.ByteBufferIterator writableByteBuffers()

Returns a closeable iterator over each ByteBuffer in 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:DataBuffer.ByteBufferIterator

a closeable iterator over the writable byte buffers contained in this data buffer

Since
6.0.5
writableByteCountback to summary
public int writableByteCount()

Return the number of bytes that can be written to this data buffer.

Returns:int

the writable byte count

Since
5.0.1
writeback to summary
public DataBuffer write(byte b)

Write a single byte into this buffer at the current writing position.

Parameters
b:byte

the byte to be written

Returns:DataBuffer

this buffer

writeback to summary
public DataBuffer write(byte[] source)

Write the given source into this buffer, starting at the current writing position of this buffer.

Parameters
source:byte[]

the bytes to be written into this buffer

Returns:DataBuffer

this buffer

writeback to summary
public 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.

Parameters
source:byte[]

the bytes to be written into this buffer

offset:int

the index within source to start writing from

length:int

the maximum number of bytes to be written from source

Returns:DataBuffer

this buffer

writeback to summary
public DataBuffer write(DataBuffer... buffers)

Write one or more DataBuffers to this buffer, starting at the current writing position. It is the responsibility of the caller to release the given data buffers.

Parameters
buffers:DataBuffer[]

the byte buffers to write into this buffer

Returns:DataBuffer

this buffer

writeback to summary
public DataBuffer write(ByteBuffer... buffers)

Write one or more ByteBuffer to this buffer, starting at the current writing position.

Parameters
buffers:ByteBuffer[]

the byte buffers to write into this buffer

Returns:DataBuffer

this buffer

writeback to summary
public default DataBuffer write(CharSequence charSequence, Charset charset)

Write the given CharSequence using the given Charset, starting at the current writing position.

Parameters
charSequence:CharSequence

the char sequence to write into this buffer

charset:Charset

the charset to encode the char sequence with

Returns:DataBuffer

this buffer

Since
5.1.4
writePositionback to summary
public int writePosition()

Return the position to which this buffer will write.

Returns:int

the write position

Since
5.0.1
writePositionback to summary
public DataBuffer writePosition(int writePosition)

Set the position to which this buffer will write.

Parameters
writePosition:int

the new write position

Returns:DataBuffer

this buffer

Exceptions
IndexOutOfBoundsException:
if writePosition is smaller than readPosition() or greater than capacity()
Since
5.0.1
org.springframework.core.io.buffer back to summary

public Interface DataBuffer.ByteBufferIterator

extends Iterator<ByteBuffer>, Closeable
Known Direct Implementers
org.springframework.core.io.buffer.DefaultDataBuffer.ByteBufferIterator, org.springframework.core.io.buffer.Netty5DataBuffer.BufferComponentIterator, org.springframework.core.io.buffer.NettyDataBuffer.ByteBufferIterator

A dedicated iterator type that ensures the lifecycle of iterated ByteBuffer elements. This iterator must be used in a try-with-resources clause or explicitly closed.
Since
6.0.5
See Also
DataBuffer#readableByteBuffers(), DataBuffer#writableByteBuffers()

Method Summary

Modifier and TypeMethod and Description
public void
close()

Redeclares java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
Inherited from java.util.Iterator:
forEachRemaininghasNextnextremove

Method Detail

closeback to summary
public void close()

Redeclares java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override