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
parent 27b83a480b
commit c35f90f920

View File

@ -444,19 +444,40 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder<Object, HttpMe
if (length < contentRead) {
if (!message.isChunked()) {
message.setChunked(true);
return new Object[] {message, new DefaultHttpChunk(buffer.readBytes(toRead))};
return new Object[] {message, new DefaultHttpChunk(read(buffer, toRead))};
} else {
return new DefaultHttpChunk(buffer.readBytes(toRead));
return new DefaultHttpChunk(read(buffer, toRead));
}
}
if (content == null) {
content = buffer.readBytes((int) length);
content = read(buffer, (int) length);
} else {
content.writeBytes(buffer.readBytes((int) length));
}
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 {
headerSize = 0;
final HttpMessage message = this.message;