Remove ByteBuf.readBytes(int) calls when possible
Motivation: We use ByteBuf.readBytes(int) in various places where we could either remove it completely or use readSlice(int).retain(). Modifications: - Remove ByteBuf.readBytes(int) when possible or replace by readSlice(int).retain(). Result: Faster code.
This commit is contained in:
parent
db74b2344f
commit
e35b7f982f
@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ReplayingDecoder;
|
||||
import io.netty.handler.codec.socks.SocksAuthRequestDecoder.State;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -60,12 +59,12 @@ public class SocksAuthRequestDecoder extends ReplayingDecoder<State> {
|
||||
}
|
||||
case READ_USERNAME: {
|
||||
fieldLength = byteBuf.readByte();
|
||||
username = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
|
||||
username = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
|
||||
checkpoint(State.READ_PASSWORD);
|
||||
}
|
||||
case READ_PASSWORD: {
|
||||
fieldLength = byteBuf.readByte();
|
||||
password = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
|
||||
password = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
|
||||
msg = new SocksAuthRequest(username, password);
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ReplayingDecoder;
|
||||
import io.netty.handler.codec.socks.SocksCmdRequestDecoder.State;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -78,13 +77,15 @@ public class SocksCmdRequestDecoder extends ReplayingDecoder<State> {
|
||||
}
|
||||
case DOMAIN: {
|
||||
fieldLength = byteBuf.readByte();
|
||||
host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
|
||||
host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
|
||||
port = byteBuf.readUnsignedShort();
|
||||
msg = new SocksCmdRequest(cmdType, addressType, host, port);
|
||||
break;
|
||||
}
|
||||
case IPv6: {
|
||||
host = SocksCommonUtils.ipv6toStr(byteBuf.readBytes(16).array());
|
||||
byte[] bytes = new byte[16];
|
||||
byteBuf.readBytes(bytes);
|
||||
host = SocksCommonUtils.ipv6toStr(bytes);
|
||||
port = byteBuf.readUnsignedShort();
|
||||
msg = new SocksCmdRequest(cmdType, addressType, host, port);
|
||||
break;
|
||||
|
@ -19,7 +19,6 @@ import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ReplayingDecoder;
|
||||
import io.netty.handler.codec.socks.SocksCmdResponseDecoder.State;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -77,13 +76,15 @@ public class SocksCmdResponseDecoder extends ReplayingDecoder<State> {
|
||||
}
|
||||
case DOMAIN: {
|
||||
fieldLength = byteBuf.readByte();
|
||||
host = byteBuf.readBytes(fieldLength).toString(CharsetUtil.US_ASCII);
|
||||
host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
|
||||
port = byteBuf.readUnsignedShort();
|
||||
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
|
||||
break;
|
||||
}
|
||||
case IPv6: {
|
||||
host = SocksCommonUtils.ipv6toStr(byteBuf.readBytes(16).array());
|
||||
byte[] bytes = new byte[16];
|
||||
byteBuf.readBytes(bytes);
|
||||
host = SocksCommonUtils.ipv6toStr(bytes);
|
||||
port = byteBuf.readUnsignedShort();
|
||||
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
|
||||
break;
|
||||
|
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package io.netty.handler.codec.socks;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
|
||||
final class SocksCommonUtils {
|
||||
@ -103,4 +105,10 @@ final class SocksCommonUtils {
|
||||
private static void appendHextet(StringBuilder sb, byte[] src, int i) {
|
||||
StringUtil.toHexString(sb, src, i << 1, 2);
|
||||
}
|
||||
|
||||
static String readUsAscii(ByteBuf buffer, int length) {
|
||||
String s = buffer.toString(buffer.readerIndex(), length, CharsetUtil.US_ASCII);
|
||||
buffer.skipBytes(length);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ public class ProtobufVarint32FrameDecoder extends ByteToMessageDecoder {
|
||||
in.resetReaderIndex();
|
||||
return;
|
||||
} else {
|
||||
out.add(in.readBytes(length));
|
||||
out.add(in.readSlice(length).retain());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user