Extract common parts from if statements (#7831)

Motivation:
Some `if` statements contains common parts that can be extracted.

Modifications:
Extract common parts from `if` statements.

Result:
Less code and bytecode. The code is simpler and more clear.
This commit is contained in:
Nikolay Fedorovskikh 2018-04-11 17:36:57 +05:00 committed by Norman Maurer
parent 76c5f6cd03
commit 401b196623
9 changed files with 34 additions and 68 deletions

View File

@ -534,12 +534,10 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
// when we produced an invalid message without consuming anything. // when we produced an invalid message without consuming anything.
in.skipBytes(in.readableBytes()); in.skipBytes(in.readableBytes());
if (message != null) { if (message == null) {
message.setDecoderResult(DecoderResult.failure(cause));
} else {
message = createInvalidMessage(); message = createInvalidMessage();
message.setDecoderResult(DecoderResult.failure(cause));
} }
message.setDecoderResult(DecoderResult.failure(cause));
HttpMessage ret = message; HttpMessage ret = message;
message = null; message = null;

View File

@ -93,6 +93,7 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
HTML5 HTML5
} }
@SuppressWarnings("rawtypes")
private static final Map.Entry[] percentEncodings; private static final Map.Entry[] percentEncodings;
static { static {
@ -779,12 +780,11 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
} }
// Now consider size for chunk or not // Now consider size for chunk or not
long realSize = globalBodySize; long realSize = globalBodySize;
if (isMultipart) { if (!isMultipart) {
iterator = multipartHttpDatas.listIterator();
} else {
realSize -= 1; // last '&' removed realSize -= 1; // last '&' removed
iterator = multipartHttpDatas.listIterator();
} }
iterator = multipartHttpDatas.listIterator();
headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(realSize)); headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(realSize));
if (realSize > HttpPostBodyUtil.chunkSize || isMultipart) { if (realSize > HttpPostBodyUtil.chunkSize || isMultipart) {
isChunked = true; isChunked = true;
@ -948,13 +948,11 @@ public class HttpPostRequestEncoder implements ChunkedInput<HttpContent> {
isKey = false; isKey = false;
if (currentBuffer == null) { if (currentBuffer == null) {
currentBuffer = wrappedBuffer(buffer, wrappedBuffer("=".getBytes())); currentBuffer = wrappedBuffer(buffer, wrappedBuffer("=".getBytes()));
// continue
size -= buffer.readableBytes() + 1;
} else { } else {
currentBuffer = wrappedBuffer(currentBuffer, buffer, wrappedBuffer("=".getBytes())); currentBuffer = wrappedBuffer(currentBuffer, buffer, wrappedBuffer("=".getBytes()));
// continue
size -= buffer.readableBytes() + 1;
} }
// continue
size -= buffer.readableBytes() + 1;
if (currentBuffer.readableBytes() >= HttpPostBodyUtil.chunkSize) { if (currentBuffer.readableBytes() >= HttpPostBodyUtil.chunkSize) {
buffer = fillByteBuf(); buffer = fillByteBuf();
return new DefaultHttpContent(buffer); return new DefaultHttpContent(buffer);

View File

@ -466,21 +466,13 @@ public class HttpPostStandardRequestDecoder implements InterfaceHttpPostRequestD
} }
firstpos = currentpos; firstpos = currentpos;
currentStatus = MultiPartStatus.EPILOGUE; currentStatus = MultiPartStatus.EPILOGUE;
undecodedChunk.readerIndex(firstpos); } else if (contRead && currentAttribute != null && currentStatus == MultiPartStatus.FIELD) {
return;
}
if (contRead && currentAttribute != null) {
// reset index except if to continue in case of FIELD getStatus // reset index except if to continue in case of FIELD getStatus
if (currentStatus == MultiPartStatus.FIELD) { currentAttribute.addContent(undecodedChunk.copy(firstpos, currentpos - firstpos),
currentAttribute.addContent(undecodedChunk.copy(firstpos, currentpos - firstpos), false);
false); firstpos = currentpos;
firstpos = currentpos;
}
undecodedChunk.readerIndex(firstpos);
} else {
// end of line or end of block so keep index to last valid position
undecodedChunk.readerIndex(firstpos);
} }
undecodedChunk.readerIndex(firstpos);
} catch (ErrorDataDecoderException e) { } catch (ErrorDataDecoderException e) {
// error while decoding // error while decoding
undecodedChunk.readerIndex(firstpos); undecodedChunk.readerIndex(firstpos);
@ -596,21 +588,13 @@ public class HttpPostStandardRequestDecoder implements InterfaceHttpPostRequestD
} }
firstpos = currentpos; firstpos = currentpos;
currentStatus = MultiPartStatus.EPILOGUE; currentStatus = MultiPartStatus.EPILOGUE;
undecodedChunk.readerIndex(firstpos); } else if (contRead && currentAttribute != null && currentStatus == MultiPartStatus.FIELD) {
return;
}
if (contRead && currentAttribute != null) {
// reset index except if to continue in case of FIELD getStatus // reset index except if to continue in case of FIELD getStatus
if (currentStatus == MultiPartStatus.FIELD) { currentAttribute.addContent(undecodedChunk.copy(firstpos, currentpos - firstpos),
currentAttribute.addContent(undecodedChunk.copy(firstpos, currentpos - firstpos), false);
false); firstpos = currentpos;
firstpos = currentpos;
}
undecodedChunk.readerIndex(firstpos);
} else {
// end of line or end of block so keep index to last valid position
undecodedChunk.readerIndex(firstpos);
} }
undecodedChunk.readerIndex(firstpos);
} catch (ErrorDataDecoderException e) { } catch (ErrorDataDecoderException e) {
// error while decoding // error while decoding
undecodedChunk.readerIndex(firstpos); undecodedChunk.readerIndex(firstpos);

View File

@ -327,11 +327,10 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
replayable.terminate(); replayable.terminate();
if (cumulation != null) { if (cumulation != null) {
callDecode(ctx, internalBuffer(), out); callDecode(ctx, internalBuffer(), out);
decodeLast(ctx, replayable, out);
} else { } else {
replayable.setCumulation(Unpooled.EMPTY_BUFFER); replayable.setCumulation(Unpooled.EMPTY_BUFFER);
decodeLast(ctx, replayable, out);
} }
decodeLast(ctx, replayable, out);
} catch (Signal replay) { } catch (Signal replay) {
// Ignore // Ignore
replay.expect(REPLAY); replay.expect(REPLAY);

View File

@ -285,26 +285,20 @@ public abstract class ReferenceCountedOpenSslContext extends SslContext implemen
} }
/* Set session cache size, if specified */ /* Set session cache size, if specified */
if (sessionCacheSize > 0) { if (sessionCacheSize <= 0) {
this.sessionCacheSize = sessionCacheSize;
SSLContext.setSessionCacheSize(ctx, sessionCacheSize);
} else {
// Get the default session cache size using SSLContext.setSessionCacheSize() // Get the default session cache size using SSLContext.setSessionCacheSize()
this.sessionCacheSize = sessionCacheSize = SSLContext.setSessionCacheSize(ctx, 20480); sessionCacheSize = SSLContext.setSessionCacheSize(ctx, 20480);
// Revert the session cache size to the default value.
SSLContext.setSessionCacheSize(ctx, sessionCacheSize);
} }
this.sessionCacheSize = sessionCacheSize;
SSLContext.setSessionCacheSize(ctx, sessionCacheSize);
/* Set session timeout, if specified */ /* Set session timeout, if specified */
if (sessionTimeout > 0) { if (sessionTimeout <= 0) {
this.sessionTimeout = sessionTimeout;
SSLContext.setSessionCacheTimeout(ctx, sessionTimeout);
} else {
// Get the default session timeout using SSLContext.setSessionCacheTimeout() // Get the default session timeout using SSLContext.setSessionCacheTimeout()
this.sessionTimeout = sessionTimeout = SSLContext.setSessionCacheTimeout(ctx, 300); sessionTimeout = SSLContext.setSessionCacheTimeout(ctx, 300);
// Revert the session timeout to the default value.
SSLContext.setSessionCacheTimeout(ctx, sessionTimeout);
} }
this.sessionTimeout = sessionTimeout;
SSLContext.setSessionCacheTimeout(ctx, sessionTimeout);
if (enableOcsp) { if (enableOcsp) {
SSLContext.enableOcsp(ctx, isClient()); SSLContext.enableOcsp(ctx, isClient());

View File

@ -140,13 +140,11 @@ public class WriteTimeoutHandler extends ChannelOutboundHandlerAdapter {
} }
private void addWriteTimeoutTask(WriteTimeoutTask task) { private void addWriteTimeoutTask(WriteTimeoutTask task) {
if (lastTask == null) { if (lastTask != null) {
lastTask = task;
} else {
lastTask.next = task; lastTask.next = task;
task.prev = lastTask; task.prev = lastTask;
lastTask = task;
} }
lastTask = task;
} }
private void removeWriteTimeoutTask(WriteTimeoutTask task) { private void removeWriteTimeoutTask(WriteTimeoutTask task) {

View File

@ -307,12 +307,10 @@ public class NioSctpChannel extends AbstractNioMessageChannel implements io.nett
} }
} }
ByteBuffer nioData; ByteBuffer nioData;
if (!needsCopy) { if (needsCopy) {
nioData = data.nioBuffer();
} else {
data = alloc.directBuffer(dataLen).writeBytes(data); data = alloc.directBuffer(dataLen).writeBytes(data);
nioData = data.nioBuffer();
} }
nioData = data.nioBuffer();
final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier()); final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
mi.payloadProtocolID(packet.protocolIdentifier()); mi.payloadProtocolID(packet.protocolIdentifier());
mi.streamNumber(packet.streamIdentifier()); mi.streamNumber(packet.streamIdentifier());

View File

@ -113,12 +113,11 @@ public final class ChannelOutboundBuffer {
Entry entry = Entry.newInstance(msg, size, total(msg), promise); Entry entry = Entry.newInstance(msg, size, total(msg), promise);
if (tailEntry == null) { if (tailEntry == null) {
flushedEntry = null; flushedEntry = null;
tailEntry = entry;
} else { } else {
Entry tail = tailEntry; Entry tail = tailEntry;
tail.next = entry; tail.next = entry;
tailEntry = entry;
} }
tailEntry = entry;
if (unflushedEntry == null) { if (unflushedEntry == null) {
unflushedEntry = entry; unflushedEntry = entry;
} }

View File

@ -130,15 +130,13 @@ class NioDatagramChannelConfig extends DefaultDatagramChannelConfig {
@Override @Override
public InetAddress getInterface() { public InetAddress getInterface() {
NetworkInterface inf = getNetworkInterface(); NetworkInterface inf = getNetworkInterface();
if (inf == null) { if (inf != null) {
return null;
} else {
Enumeration<InetAddress> addresses = SocketUtils.addressesFromNetworkInterface(inf); Enumeration<InetAddress> addresses = SocketUtils.addressesFromNetworkInterface(inf);
if (addresses.hasMoreElements()) { if (addresses.hasMoreElements()) {
return addresses.nextElement(); return addresses.nextElement();
} }
return null;
} }
return null;
} }
@Override @Override