Port enhancement to reduce memory copy if possible. See #412

This commit is contained in:
Norman Maurer 2012-06-29 13:26:13 +02:00 committed by Trustin Lee
parent efce2624dd
commit 8224c95e05

View File

@ -444,19 +444,40 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder<Object, HttpMe
if (length < contentRead) { if (length < contentRead) {
if (!message.isChunked()) { if (!message.isChunked()) {
message.setChunked(true); message.setChunked(true);
return new Object[] {message, new DefaultHttpChunk(buffer.readBytes(toRead))}; return new Object[] {message, new DefaultHttpChunk(read(buffer, toRead))};
} else { } else {
return new DefaultHttpChunk(buffer.readBytes(toRead)); return new DefaultHttpChunk(read(buffer, toRead));
} }
} }
if (content == null) { if (content == null) {
content = buffer.readBytes((int) length); content = read(buffer, (int) length);
} else { } else {
content.writeBytes(buffer.readBytes((int) length)); content.writeBytes(buffer.readBytes((int) length));
} }
return reset(); return reset();
} }
/**
* Try to do an optimized "read" of len from the given {@link ByteBuf}.
*
* This is part of #412 to safe byte copies
*
*/
private ByteBuf read(ByteBuf buffer, int len) {
ByteBuf internal = internalBuffer();
if (internal.readableBytes() >= len) {
int index = internal.readerIndex();
ByteBuf buf = internal.slice(index, len);
// update the readerindex so an the next read its on the correct position
buffer.readerIndex(index + len);
return buf;
} else {
return buffer.readBytes(len);
}
}
private State readHeaders(ByteBuf buffer) throws TooLongFrameException { private State readHeaders(ByteBuf buffer) throws TooLongFrameException {
headerSize = 0; headerSize = 0;
final HttpMessage message = this.message; final HttpMessage message = this.message;