Added allocator property to DataBuffer

This commit is contained in:
Arjen Poutsma 2016-02-23 10:53:43 +01:00
parent 273c1b85e6
commit b838fdab88
5 changed files with 35 additions and 11 deletions

View File

@ -27,6 +27,12 @@ import java.nio.ByteBuffer;
*/ */
public interface DataBuffer { public interface DataBuffer {
/**
* Returns the {@link DataBufferAllocator} that created this buffer.
* @return the creating buffer allocator
*/
DataBufferAllocator allocator();
/** /**
* Gets the byte at the specified index. * Gets the byte at the specified index.
* @param index the index * @param index the index

View File

@ -36,6 +36,8 @@ import org.springframework.util.ObjectUtils;
*/ */
public class DefaultDataBuffer implements DataBuffer { public class DefaultDataBuffer implements DataBuffer {
private final DefaultDataBufferAllocator allocator;
private ByteBuffer byteBuffer; private ByteBuffer byteBuffer;
private int readPosition; private int readPosition;
@ -48,20 +50,27 @@ public class DefaultDataBuffer implements DataBuffer {
* ByteBuffer#position() position} of the given buffer. * ByteBuffer#position() position} of the given buffer.
* @param byteBuffer the buffer to base this buffer on * @param byteBuffer the buffer to base this buffer on
*/ */
DefaultDataBuffer(ByteBuffer byteBuffer) { DefaultDataBuffer(ByteBuffer byteBuffer, DefaultDataBufferAllocator allocator) {
this(byteBuffer, byteBuffer.position(), byteBuffer.position()); this(byteBuffer, byteBuffer.position(), byteBuffer.position(), allocator);
} }
DefaultDataBuffer(ByteBuffer byteBuffer, int readPosition, int writePosition) { DefaultDataBuffer(ByteBuffer byteBuffer, int readPosition, int writePosition, DefaultDataBufferAllocator allocator) {
Assert.notNull(byteBuffer, "'byteBuffer' must not be null"); Assert.notNull(byteBuffer, "'byteBuffer' must not be null");
Assert.isTrue(readPosition >= 0, "'readPosition' must be 0 or higher"); Assert.isTrue(readPosition >= 0, "'readPosition' must be 0 or higher");
Assert.isTrue(writePosition >= 0, "'writePosition' must be 0 or higher"); Assert.isTrue(writePosition >= 0, "'writePosition' must be 0 or higher");
Assert.isTrue(readPosition <= writePosition, Assert.isTrue(readPosition <= writePosition,
"'readPosition' must be smaller than or equal to 'writePosition'"); "'readPosition' must be smaller than or equal to 'writePosition'");
Assert.notNull(allocator, "'allocator' must not be null");
this.byteBuffer = byteBuffer; this.byteBuffer = byteBuffer;
this.readPosition = readPosition; this.readPosition = readPosition;
this.writePosition = writePosition; this.writePosition = writePosition;
this.allocator = allocator;
}
@Override
public DefaultDataBufferAllocator allocator() {
return this.allocator;
} }
/** /**

View File

@ -80,14 +80,14 @@ public class DefaultDataBufferAllocator implements DataBufferAllocator {
@Override @Override
public DefaultDataBuffer allocateBuffer(int initialCapacity) { public DefaultDataBuffer allocateBuffer(int initialCapacity) {
return this.preferDirect ? return this.preferDirect ?
new DefaultDataBuffer(ByteBuffer.allocateDirect(initialCapacity)) : new DefaultDataBuffer(ByteBuffer.allocateDirect(initialCapacity), this) :
new DefaultDataBuffer(ByteBuffer.allocate(initialCapacity)); new DefaultDataBuffer(ByteBuffer.allocate(initialCapacity), this);
} }
@Override @Override
public DefaultDataBuffer wrap(ByteBuffer byteBuffer) { public DefaultDataBuffer wrap(ByteBuffer byteBuffer) {
ByteBuffer sliced = byteBuffer.slice(); ByteBuffer sliced = byteBuffer.slice();
return new DefaultDataBuffer(sliced, 0, byteBuffer.remaining()); return new DefaultDataBuffer(sliced, 0, byteBuffer.remaining(), this);
} }
@Override @Override

View File

@ -38,16 +38,25 @@ import org.springframework.util.ObjectUtils;
*/ */
public class NettyDataBuffer implements DataBuffer { public class NettyDataBuffer implements DataBuffer {
private final NettyDataBufferAllocator allocator;
private ByteBuf byteBuf; private ByteBuf byteBuf;
/** /**
* Creates a new {@code NettyDataBuffer} based on the given {@code ByteBuff}. * Creates a new {@code NettyDataBuffer} based on the given {@code ByteBuff}.
* @param byteBuf the buffer to base this buffer on * @param byteBuf the buffer to base this buffer on
*/ */
public NettyDataBuffer(ByteBuf byteBuf) { NettyDataBuffer(ByteBuf byteBuf, NettyDataBufferAllocator allocator) {
Assert.notNull(byteBuf, "'byteBuf' must not be null"); Assert.notNull(byteBuf, "'byteBuf' must not be null");
Assert.notNull(allocator, "'allocator' must not be null");
this.byteBuf = byteBuf; this.byteBuf = byteBuf;
this.allocator = allocator;
}
@Override
public NettyDataBufferAllocator allocator() {
return allocator;
} }
/** /**

View File

@ -51,19 +51,19 @@ public class NettyDataBufferAllocator implements DataBufferAllocator {
@Override @Override
public NettyDataBuffer allocateBuffer() { public NettyDataBuffer allocateBuffer() {
ByteBuf byteBuf = this.byteBufAllocator.buffer(); ByteBuf byteBuf = this.byteBufAllocator.buffer();
return new NettyDataBuffer(byteBuf); return new NettyDataBuffer(byteBuf, this);
} }
@Override @Override
public NettyDataBuffer allocateBuffer(int initialCapacity) { public NettyDataBuffer allocateBuffer(int initialCapacity) {
ByteBuf byteBuf = this.byteBufAllocator.buffer(initialCapacity); ByteBuf byteBuf = this.byteBufAllocator.buffer(initialCapacity);
return new NettyDataBuffer(byteBuf); return new NettyDataBuffer(byteBuf, this);
} }
@Override @Override
public NettyDataBuffer wrap(ByteBuffer byteBuffer) { public NettyDataBuffer wrap(ByteBuffer byteBuffer) {
ByteBuf byteBuf = Unpooled.wrappedBuffer(byteBuffer); ByteBuf byteBuf = Unpooled.wrappedBuffer(byteBuffer);
return new NettyDataBuffer(byteBuf); return new NettyDataBuffer(byteBuf, this);
} }
/** /**
@ -72,7 +72,7 @@ public class NettyDataBufferAllocator implements DataBufferAllocator {
* @return the wrapped buffer * @return the wrapped buffer
*/ */
public NettyDataBuffer wrap(ByteBuf byteBuf) { public NettyDataBuffer wrap(ByteBuf byteBuf) {
return new NettyDataBuffer(byteBuf); return new NettyDataBuffer(byteBuf, this);
} }
@Override @Override