Add DataBuffer.retainedSlice

Add method retainedSlice to the DataBuffer, defaulting to using
DataBufferUtils for retain, but allowing for ByteBuf specific override.
This commit is contained in:
Arjen Poutsma
2019-05-06 15:49:51 +02:00
parent 1d80cbea35
commit b74c09d12e
3 changed files with 54 additions and 0 deletions

View File

@@ -289,6 +289,23 @@ public interface DataBuffer {
*/
DataBuffer slice(int index, int length);
/**
* Create a new {@code 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.
* <p><strong>Note</strong> that unlike {@link #slice(int, int)}, this method
* <strong>will</strong> call {@link DataBufferUtils#retain(DataBuffer)} (or equivalent) on the
* resulting slice.
* @param index the index at which to start the slice
* @param length the length of the slice
* @return the specified, retained slice of this data buffer
* @since 5.2
*/
default DataBuffer retainedSlice(int index, int length) {
return DataBufferUtils.retain(slice(index, length));
}
/**
* Expose this buffer's bytes as a {@link ByteBuffer}. Data between this
* {@code DataBuffer} and the returned {@code ByteBuffer} is shared; though

View File

@@ -261,6 +261,12 @@ public class NettyDataBuffer implements PooledDataBuffer {
return new NettyDataBuffer(slice, this.dataBufferFactory);
}
@Override
public NettyDataBuffer retainedSlice(int index, int length) {
ByteBuf slice = this.byteBuf.retainedSlice(index, length);
return new NettyDataBuffer(slice, this.dataBufferFactory);
}
@Override
public ByteBuffer asByteBuffer() {
return this.byteBuf.nioBuffer();