Skip to content

Commit

Permalink
Convert fields to the local variable when possible
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fenik17 authored and Scottmitch committed Mar 9, 2017
1 parent 10d9f82 commit f49bf4b
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ private enum State {
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;

Expand Down Expand Up @@ -99,6 +98,7 @@ protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ enum DecoderState {

private MqttFixedHeader mqttFixedHeader;
private Object variableHeader;
private Object payload;
private int bytesRemainingInVariablePart;

private final int maxBytesInMessage;
Expand Down Expand Up @@ -99,18 +98,17 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> ou
mqttFixedHeader.messageType(),
bytesRemainingInVariablePart,
variableHeader);
payload = decodedPayload.value;
bytesRemainingInVariablePart -= decodedPayload.numberOfBytesConsumed;
if (bytesRemainingInVariablePart != 0) {
throw new DecoderException(
"non-zero remaining payload bytes: " +
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
*/
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);
Expand All @@ -42,25 +38,28 @@ public SocksAuthRequestDecoder() {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
*/
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);
}
Expand All @@ -41,19 +37,22 @@ protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteB
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,8 @@
*/
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);
Expand All @@ -46,48 +39,56 @@ public SocksCmdRequestDecoder() {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@
*/
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);
Expand All @@ -45,48 +39,56 @@ public SocksCmdResponseDecoder() {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.netty.handler.codec.socks.SocksInitRequestDecoder.State;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -29,11 +30,6 @@
*/
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);
}
Expand All @@ -42,24 +38,31 @@ public SocksInitRequestDecoder() {
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();
for (int i = 0; i < authSchemeNum; i++) {
authSchemes.add(SocksAuthScheme.valueOf(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()));
}
} else {
authSchemes = Collections.emptyList();
}
msg = new SocksInitRequest(authSchemes);
out.add(new SocksInitRequest(authSchemes));
break;
}
default: {
throw new Error();
}
}
ctx.pipeline().remove(this);
out.add(msg);
}

enum State {
Expand Down
Loading

0 comments on commit f49bf4b

Please sign in to comment.