Use ByteProcessor.FIND_LF to find end of line
Motivation: Each call of ByteBuf.getByte(int) method does boundary checking. This can be eliminated by using ByteBuf.forEachByte(ByteProcessor) method and ByteProcessor.FIND_LF processor. Modifications: Find end of line with ByteProcessor.FIND_LF Result: A little better performance of LineBasedFrameDecoder.
This commit is contained in:
parent
a4846e6153
commit
a2e113b987
|
@ -17,6 +17,7 @@ package io.netty.handler.codec;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.util.ByteProcessor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -152,15 +153,10 @@ public class LineBasedFrameDecoder extends ByteToMessageDecoder {
|
||||||
* Returns -1 if no end of line was found in the buffer.
|
* Returns -1 if no end of line was found in the buffer.
|
||||||
*/
|
*/
|
||||||
private static int findEndOfLine(final ByteBuf buffer) {
|
private static int findEndOfLine(final ByteBuf buffer) {
|
||||||
final int n = buffer.writerIndex();
|
int i = buffer.forEachByte(ByteProcessor.FIND_LF);
|
||||||
for (int i = buffer.readerIndex(); i < n; i ++) {
|
if (i > 0 && buffer.getByte(i - 1) == '\r') {
|
||||||
final byte b = buffer.getByte(i);
|
i--;
|
||||||
if (b == '\n') {
|
|
||||||
return i;
|
|
||||||
} else if (b == '\r' && i < n - 1 && buffer.getByte(i + 1) == '\n') {
|
|
||||||
return i; // \r\n
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return -1; // Not found.
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user