From 759d0633a4c6ea2262833e43d37adf0a62ab8a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Mon, 30 Apr 2012 12:38:07 +0300 Subject: [PATCH] Add the SeekAheadOptimize class to enable faster seek of bytes values in HttpPostRequestDecoder --- .../handler/codec/http/HttpPostBodyUtil.java | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostBodyUtil.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostBodyUtil.java index e74e1ecdc0..b1ceca7bf0 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostBodyUtil.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostBodyUtil.java @@ -116,18 +116,58 @@ final class HttpPostBodyUtil { private HttpPostBodyUtil() { } - //Some commons methods between HttpPostRequestDecoder and HttpMessageDecoder /** - * Skip control Characters - * @param buffer + * Exception when NO Backend Array is found + */ + static class SeekAheadNoBackArray extends Exception { + private static final long serialVersionUID = -630418804938699495L; + } + + /** + * This class intends to decrease the CPU in seeking ahead some bytes in + * HttpPostRequestDecoder */ - static void skipControlCharacters(ChannelBuffer buffer) { - for (;;) { - char c = (char) buffer.readUnsignedByte(); - if (!Character.isISOControl(c) && !Character.isWhitespace(c)) { - buffer.readerIndex(buffer.readerIndex() - 1); - break; + static class SeekAheadOptimize { + byte[] bytes; + + int readerIndex; + + int pos; + + int limit; + + ChannelBuffer buffer; + + /** + * @param buffer + */ + SeekAheadOptimize(ChannelBuffer buffer) + throws SeekAheadNoBackArray { + if (! buffer.hasArray()) { + throw new SeekAheadNoBackArray(); } + this.buffer = buffer; + this.bytes = buffer.array(); + this.pos = this.readerIndex = buffer.readerIndex(); + this.limit = buffer.writerIndex(); + } + /** + * + * @param minus this value will be used as (currentPos - minus) to set + * the current readerIndex in the buffer. + */ + void setReadPosition(int minus) { + pos -= minus; + readerIndex = pos; + buffer.readerIndex(readerIndex); + } + + void clear() { + this.buffer = null; + this.bytes = null; + this.limit = 0; + this.pos = 0; + this.readerIndex = 0; } }