Fix bug in DefaultChannelHandlerContext where ByteBridge does not handle bounded buffers well

This commit is contained in:
Norman Maurer 2012-12-10 16:47:24 +01:00
parent 321b18d4d1
commit 3e783e1b5e

View File

@ -1237,23 +1237,30 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
data = ctx.alloc().buffer(dataLen, dataLen); data = ctx.alloc().buffer(dataLen, dataLen);
} }
byteBuf.readBytes(data); byteBuf.readBytes(data, dataLen);
byteBuf.unsafe().discardSomeReadBytes(); byteBuf.unsafe().discardSomeReadBytes();
exchangeBuf.add(data); exchangeBuf.add(data);
} }
private void flush(ByteBuf out) { private void flush(ByteBuf out) {
for (;;) { while (out.writable()) {
ByteBuf data = exchangeBuf.poll(); ByteBuf data = exchangeBuf.peek();
if (data == null) { if (data == null) {
break; break;
} }
try { if (out.writerIndex() > out.maxCapacity() - data.readableBytes()) {
out.writeBytes(data); // The target buffer is not going to be able to accept all data in the bridge.
} finally { out.ensureWritableBytes(out.maxCapacity() - out.writerIndex());
data.unsafe().free(); out.writeBytes(data, out.writableBytes());
} else {
exchangeBuf.remove();
try {
out.writeBytes(data);
} finally {
data.unsafe().free();
}
} }
} }
} }