Use existing CompositeByteBuf if possible

This commit uses the existing CompositeByteBuf if present, as opposed
to creating a new composite for every call to
NettyDataBuffer.write(ByteBuf...)

Issue: SPR-16180
This commit is contained in:
Arjen Poutsma 2017-11-10 14:47:39 +01:00
parent 8cfa3c632b
commit 8223809455
1 changed files with 12 additions and 10 deletions

View File

@ -208,16 +208,18 @@ public class NettyDataBuffer implements PooledDataBuffer {
public NettyDataBuffer write(ByteBuf... byteBufs) { public NettyDataBuffer write(ByteBuf... byteBufs) {
Assert.notNull(byteBufs, "'byteBufs' must not be null"); Assert.notNull(byteBufs, "'byteBufs' must not be null");
CompositeByteBuf composite = new CompositeByteBuf( if (this.byteBuf instanceof CompositeByteBuf) {
this.byteBuf.alloc(), this.byteBuf.isDirect(), byteBufs.length + 1); CompositeByteBuf composite = (CompositeByteBuf) this.byteBuf;
composite.addComponent(this.byteBuf); composite.addComponents(true, byteBufs);
composite.addComponents(byteBufs); }
else {
int writerIndex = this.byteBuf.readableBytes() + ByteBuf oldByteBuf = this.byteBuf;
Arrays.stream(byteBufs).mapToInt(ByteBuf::readableBytes).sum(); CompositeByteBuf composite = oldByteBuf.alloc().compositeBuffer(byteBufs.length + 1);
composite.writerIndex(writerIndex); composite.addComponent(true, oldByteBuf);
composite.addComponents(true, byteBufs);
this.byteBuf = composite; this.byteBuf = composite;
}
return this; return this;
} }
@ -249,7 +251,7 @@ public class NettyDataBuffer implements PooledDataBuffer {
@Override @Override
public PooledDataBuffer retain() { public PooledDataBuffer retain() {
return new NettyDataBuffer(this.byteBuf.retain(), dataBufferFactory); return new NettyDataBuffer(this.byteBuf.retain(), this.dataBufferFactory);
} }
@Override @Override