Slice the buffer if possible to reduce memory copies when reading the content. See #412
This commit is contained in:
parent
215180511d
commit
83148869aa
@ -447,19 +447,34 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder<HttpMessageDec
|
|||||||
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 todo an optimized "read" of len from the given {@link ChannelBuffer}.
|
||||||
|
*
|
||||||
|
* This is part of #412 to safe byte copies
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private ChannelBuffer read(ChannelBuffer buffer, int len) {
|
||||||
|
ChannelBuffer internal = internalBuffer();
|
||||||
|
if (internal.readableBytes() >= len) {
|
||||||
|
return internal.slice(internal.readerIndex(), len);
|
||||||
|
} else {
|
||||||
|
return buffer.readBytes(len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private State readHeaders(ChannelBuffer buffer) throws TooLongFrameException {
|
private State readHeaders(ChannelBuffer buffer) throws TooLongFrameException {
|
||||||
headerSize = 0;
|
headerSize = 0;
|
||||||
final HttpMessage message = this.message;
|
final HttpMessage message = this.message;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user