Convert fields to the local variable when possible

Motivation:

Some classes have fields which can be local.

Modifications:

Convert fields to the local variable when possible.

Result:

Clean up. More chances for young generation or scalar replacement.
This commit is contained in:
Nikolay Fedorovskikh 2017-03-08 22:03:23 +05:00 committed by Scott Mitchell
parent 10d9f82f14
commit f49bf4b201
9 changed files with 127 additions and 95 deletions

View File

@ -61,7 +61,6 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
private static final int CONTINUE_CODE = HttpResponseStatus.CONTINUE.code();
private final Queue<CharSequence> acceptEncodingQueue = new ArrayDeque<CharSequence>();
private CharSequence acceptEncoding;
private EmbeddedChannel encoder;
private State state = State.AWAIT_HEADERS;
@ -99,6 +98,7 @@ public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpReque
final HttpResponse res = (HttpResponse) msg;
final int code = res.status().code();
final CharSequence acceptEncoding;
if (code == CONTINUE_CODE) {
// We need to not poll the encoding when response with CONTINUE as another response will follow
// for the issued request. See https://github.com/netty/netty/issues/4079

View File

@ -55,7 +55,6 @@ public final class MqttDecoder extends ReplayingDecoder<DecoderState> {
private MqttFixedHeader mqttFixedHeader;
private Object variableHeader;
private Object payload;
private int bytesRemainingInVariablePart;
private final int maxBytesInMessage;
@ -99,7 +98,6 @@ public final class MqttDecoder extends ReplayingDecoder<DecoderState> {
mqttFixedHeader.messageType(),
bytesRemainingInVariablePart,
variableHeader);
payload = decodedPayload.value;
bytesRemainingInVariablePart -= decodedPayload.numberOfBytesConsumed;
if (bytesRemainingInVariablePart != 0) {
throw new DecoderException(
@ -107,10 +105,10 @@ public final class MqttDecoder extends ReplayingDecoder<DecoderState> {
bytesRemainingInVariablePart + " (" + mqttFixedHeader.messageType() + ')');
}
checkpoint(DecoderState.READ_FIXED_HEADER);
MqttMessage message = MqttMessageFactory.newMessage(mqttFixedHeader, variableHeader, payload);
MqttMessage message = MqttMessageFactory.newMessage(
mqttFixedHeader, variableHeader, decodedPayload.value);
mqttFixedHeader = null;
variableHeader = null;
payload = null;
out.add(message);
break;
} catch (Exception cause) {

View File

@ -28,11 +28,7 @@ import java.util.List;
*/
public class SocksAuthRequestDecoder extends ReplayingDecoder<State> {
private SocksSubnegotiationVersion version;
private int fieldLength;
private String username;
private String password;
private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
public SocksAuthRequestDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
@ -42,25 +38,28 @@ public class SocksAuthRequestDecoder extends ReplayingDecoder<State> {
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksSubnegotiationVersion.valueOf(byteBuf.readByte());
if (version != SocksSubnegotiationVersion.AUTH_PASSWORD) {
if (byteBuf.readByte() != SocksSubnegotiationVersion.AUTH_PASSWORD.byteValue()) {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
break;
}
checkpoint(State.READ_USERNAME);
}
case READ_USERNAME: {
fieldLength = byteBuf.readByte();
int fieldLength = byteBuf.readByte();
username = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
checkpoint(State.READ_PASSWORD);
}
case READ_PASSWORD: {
fieldLength = byteBuf.readByte();
password = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
msg = new SocksAuthRequest(username, password);
int fieldLength = byteBuf.readByte();
String password = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
out.add(new SocksAuthRequest(username, password));
break;
}
default: {
throw new Error();
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {

View File

@ -28,10 +28,6 @@ import java.util.List;
*/
public class SocksAuthResponseDecoder extends ReplayingDecoder<State> {
private SocksSubnegotiationVersion version;
private SocksAuthStatus authStatus;
private SocksResponse msg = SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE;
public SocksAuthResponseDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@ -41,19 +37,22 @@ public class SocksAuthResponseDecoder extends ReplayingDecoder<State> {
throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksSubnegotiationVersion.valueOf(byteBuf.readByte());
if (version != SocksSubnegotiationVersion.AUTH_PASSWORD) {
if (byteBuf.readByte() != SocksSubnegotiationVersion.AUTH_PASSWORD.byteValue()) {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
break;
}
checkpoint(State.READ_AUTH_RESPONSE);
}
case READ_AUTH_RESPONSE: {
authStatus = SocksAuthStatus.valueOf(byteBuf.readByte());
msg = new SocksAuthResponse(authStatus);
SocksAuthStatus authStatus = SocksAuthStatus.valueOf(byteBuf.readByte());
out.add(new SocksAuthResponse(authStatus));
break;
}
default: {
throw new Error();
}
}
channelHandlerContext.pipeline().remove(this);
out.add(msg);
}
enum State {

View File

@ -28,15 +28,8 @@ import java.util.List;
*/
public class SocksCmdRequestDecoder extends ReplayingDecoder<State> {
private SocksProtocolVersion version;
private int fieldLength;
private SocksCmdType cmdType;
private SocksAddressType addressType;
@SuppressWarnings("UnusedDeclaration")
private byte reserved;
private String host;
private int port;
private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
public SocksCmdRequestDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
@ -46,48 +39,56 @@ public class SocksCmdRequestDecoder extends ReplayingDecoder<State> {
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
break;
}
checkpoint(State.READ_CMD_HEADER);
}
case READ_CMD_HEADER: {
cmdType = SocksCmdType.valueOf(byteBuf.readByte());
reserved = byteBuf.readByte();
byteBuf.skipBytes(1); // reserved
addressType = SocksAddressType.valueOf(byteBuf.readByte());
checkpoint(State.READ_CMD_ADDRESS);
}
case READ_CMD_ADDRESS: {
switch (addressType) {
case IPv4: {
host = SocksCommonUtils.intToIp(byteBuf.readInt());
port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port);
String host = SocksCommonUtils.intToIp(byteBuf.readInt());
int port = byteBuf.readUnsignedShort();
out.add(new SocksCmdRequest(cmdType, addressType, host, port));
break;
}
case DOMAIN: {
fieldLength = byteBuf.readByte();
host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port);
int fieldLength = byteBuf.readByte();
String host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
int port = byteBuf.readUnsignedShort();
out.add(new SocksCmdRequest(cmdType, addressType, host, port));
break;
}
case IPv6: {
byte[] bytes = new byte[16];
byteBuf.readBytes(bytes);
host = SocksCommonUtils.ipv6toStr(bytes);
port = byteBuf.readUnsignedShort();
msg = new SocksCmdRequest(cmdType, addressType, host, port);
String host = SocksCommonUtils.ipv6toStr(bytes);
int port = byteBuf.readUnsignedShort();
out.add(new SocksCmdRequest(cmdType, addressType, host, port));
break;
}
case UNKNOWN:
case UNKNOWN: {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
break;
}
default: {
throw new Error();
}
}
break;
}
default: {
throw new Error();
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {

View File

@ -28,14 +28,8 @@ import java.util.List;
*/
public class SocksCmdResponseDecoder extends ReplayingDecoder<State> {
private SocksProtocolVersion version;
private int fieldLength;
private SocksCmdStatus cmdStatus;
private SocksAddressType addressType;
private byte reserved;
private String host;
private int port;
private SocksResponse msg = SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE;
public SocksCmdResponseDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
@ -45,48 +39,56 @@ public class SocksCmdResponseDecoder extends ReplayingDecoder<State> {
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
break;
}
checkpoint(State.READ_CMD_HEADER);
}
case READ_CMD_HEADER: {
cmdStatus = SocksCmdStatus.valueOf(byteBuf.readByte());
reserved = byteBuf.readByte();
byteBuf.skipBytes(1); // reserved
addressType = SocksAddressType.valueOf(byteBuf.readByte());
checkpoint(State.READ_CMD_ADDRESS);
}
case READ_CMD_ADDRESS: {
switch (addressType) {
case IPv4: {
host = SocksCommonUtils.intToIp(byteBuf.readInt());
port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
String host = SocksCommonUtils.intToIp(byteBuf.readInt());
int port = byteBuf.readUnsignedShort();
out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
break;
}
case DOMAIN: {
fieldLength = byteBuf.readByte();
host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
int fieldLength = byteBuf.readByte();
String host = SocksCommonUtils.readUsAscii(byteBuf, fieldLength);
int port = byteBuf.readUnsignedShort();
out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
break;
}
case IPv6: {
byte[] bytes = new byte[16];
byteBuf.readBytes(bytes);
host = SocksCommonUtils.ipv6toStr(bytes);
port = byteBuf.readUnsignedShort();
msg = new SocksCmdResponse(cmdStatus, addressType, host, port);
String host = SocksCommonUtils.ipv6toStr(bytes);
int port = byteBuf.readUnsignedShort();
out.add(new SocksCmdResponse(cmdStatus, addressType, host, port));
break;
}
case UNKNOWN:
case UNKNOWN: {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
break;
}
default: {
throw new Error();
}
}
break;
}
default: {
throw new Error();
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {

View File

@ -21,6 +21,7 @@ import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socks.SocksInitRequestDecoder.State;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -29,11 +30,6 @@ import java.util.List;
*/
public class SocksInitRequestDecoder extends ReplayingDecoder<State> {
private final List<SocksAuthScheme> authSchemes = new ArrayList<SocksAuthScheme>();
private SocksProtocolVersion version;
private byte authSchemeNum;
private SocksRequest msg = SocksCommonUtils.UNKNOWN_SOCKS_REQUEST;
public SocksInitRequestDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@ -42,24 +38,31 @@ public class SocksInitRequestDecoder extends ReplayingDecoder<State> {
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_REQUEST);
break;
}
checkpoint(State.READ_AUTH_SCHEMES);
}
case READ_AUTH_SCHEMES: {
authSchemes.clear();
authSchemeNum = byteBuf.readByte();
final byte authSchemeNum = byteBuf.readByte();
final List<SocksAuthScheme> authSchemes;
if (authSchemeNum > 0) {
authSchemes = new ArrayList<SocksAuthScheme>(authSchemeNum);
for (int i = 0; i < authSchemeNum; i++) {
authSchemes.add(SocksAuthScheme.valueOf(byteBuf.readByte()));
}
msg = new SocksInitRequest(authSchemes);
} else {
authSchemes = Collections.emptyList();
}
out.add(new SocksInitRequest(authSchemes));
break;
}
default: {
throw new Error();
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {

View File

@ -28,11 +28,6 @@ import java.util.List;
*/
public class SocksInitResponseDecoder extends ReplayingDecoder<State> {
private SocksProtocolVersion version;
private SocksAuthScheme authScheme;
private SocksResponse msg = SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE;
public SocksInitResponseDecoder() {
super(State.CHECK_PROTOCOL_VERSION);
}
@ -41,20 +36,22 @@ public class SocksInitResponseDecoder extends ReplayingDecoder<State> {
protected void decode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> out) throws Exception {
switch (state()) {
case CHECK_PROTOCOL_VERSION: {
version = SocksProtocolVersion.valueOf(byteBuf.readByte());
if (version != SocksProtocolVersion.SOCKS5) {
if (byteBuf.readByte() != SocksProtocolVersion.SOCKS5.byteValue()) {
out.add(SocksCommonUtils.UNKNOWN_SOCKS_RESPONSE);
break;
}
checkpoint(State.READ_PREFFERED_AUTH_TYPE);
}
case READ_PREFFERED_AUTH_TYPE: {
authScheme = SocksAuthScheme.valueOf(byteBuf.readByte());
msg = new SocksInitResponse(authScheme);
SocksAuthScheme authScheme = SocksAuthScheme.valueOf(byteBuf.readByte());
out.add(new SocksInitResponse(authScheme));
break;
}
default: {
throw new Error();
}
}
ctx.pipeline().remove(this);
out.add(msg);
}
enum State {

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec.socks;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test;
@ -22,17 +24,48 @@ import static org.junit.Assert.*;
public class SocksAuthRequestDecoderTest {
private static final String username = "testUserName";
private static final String password = "testPassword";
@Test
public void testAuthRequestDecoder() {
String username = "test";
String password = "test";
SocksAuthRequest msg = new SocksAuthRequest(username, password);
SocksAuthRequestDecoder decoder = new SocksAuthRequestDecoder();
EmbeddedChannel embedder = new EmbeddedChannel(decoder);
SocksCommonTestUtils.writeMessageIntoEmbedder(embedder, msg);
msg = embedder.readInbound();
assertEquals(msg.username(), username);
assertEquals(msg.username(), password);
assertEquals(username, msg.username());
assertEquals(password, msg.password());
assertNull(embedder.readInbound());
}
@Test
public void testAuthRequestDecoderPartialSend() {
EmbeddedChannel ch = new EmbeddedChannel(new SocksAuthRequestDecoder());
ByteBuf byteBuf = Unpooled.buffer(16);
// Send username and password size
byteBuf.writeByte(SocksSubnegotiationVersion.AUTH_PASSWORD.byteValue());
byteBuf.writeByte(username.length());
byteBuf.writeBytes(username.getBytes());
byteBuf.writeByte(password.length());
ch.writeInbound(byteBuf);
// Check that channel is empty
assertNull(ch.readInbound());
// Send password
ByteBuf byteBuf2 = Unpooled.buffer();
byteBuf2.writeBytes(password.getBytes());
ch.writeInbound(byteBuf2);
// Read message from channel
SocksAuthRequest msg = ch.readInbound();
// Check message
assertEquals(username, msg.username());
assertEquals(password, msg.password());
assertFalse(ch.finishAndReleaseAll());
}
}