use checkPositive/checkPositiveOrZero (#8835)

Motivation:

We can replace some "hand-rolled" integer checks with our own static utility method to simplify the code.

Modifications:

Use methods provided by `ObjectUtil`.

Result:

Cleaner code and less duplication
This commit is contained in:
田欧 2019-02-04 22:55:07 +08:00 committed by Norman Maurer
parent b72fea340b
commit 4c64c98f34
24 changed files with 74 additions and 120 deletions

View File

@ -21,6 +21,7 @@ import io.netty.util.internal.UnstableApi;
import java.net.IDN; import java.net.IDN;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/** /**
* A skeletal implementation of {@link DnsRecord}. * A skeletal implementation of {@link DnsRecord}.
@ -62,9 +63,7 @@ public abstract class AbstractDnsRecord implements DnsRecord {
* @param timeToLive the TTL value of the record * @param timeToLive the TTL value of the record
*/ */
protected AbstractDnsRecord(String name, DnsRecordType type, int dnsClass, long timeToLive) { protected AbstractDnsRecord(String name, DnsRecordType type, int dnsClass, long timeToLive) {
if (timeToLive < 0) { checkPositiveOrZero(timeToLive, "timeToLive");
throw new IllegalArgumentException("timeToLive: " + timeToLive + " (expected: >= 0)");
}
// Convert to ASCII which will also check that the length is not too big. // Convert to ASCII which will also check that the length is not too big.
// See: // See:
// - https://github.com/netty/netty/issues/4937 // - https://github.com/netty/netty/issues/4937

View File

@ -372,8 +372,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
default: default:
// Check to see if the character is not an ASCII character, or invalid // Check to see if the character is not an ASCII character, or invalid
if (value < 0) { if (value < 0) {
throw new IllegalArgumentException("a header name cannot contain non-ASCII character: " + throw new IllegalArgumentException("a header name cannot contain non-ASCII character: " + value);
value);
} }
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -168,21 +170,10 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
protected HttpObjectDecoder( protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) { boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) {
if (maxInitialLineLength <= 0) { checkPositive(maxInitialLineLength, "maxInitialLineLength");
throw new IllegalArgumentException( checkPositive(maxHeaderSize, "maxHeaderSize");
"maxInitialLineLength must be a positive integer: " + checkPositive(maxChunkSize, "maxChunkSize");
maxInitialLineLength);
}
if (maxHeaderSize <= 0) {
throw new IllegalArgumentException(
"maxHeaderSize must be a positive integer: " +
maxHeaderSize);
}
if (maxChunkSize <= 0) {
throw new IllegalArgumentException(
"maxChunkSize must be a positive integer: " +
maxChunkSize);
}
AppendableCharSequence seq = new AppendableCharSequence(initialBufferSize); AppendableCharSequence seq = new AppendableCharSequence(initialBufferSize);
lineParser = new LineParser(seq, maxInitialLineLength); lineParser = new LineParser(seq, maxInitialLineLength);
headerParser = new HeaderParser(seq, maxHeaderSize); headerParser = new HeaderParser(seq, maxHeaderSize);

View File

@ -22,6 +22,7 @@ import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpConstants.SP; import static io.netty.handler.codec.http.HttpConstants.SP;
import static io.netty.util.ByteProcessor.FIND_ASCII_SPACE; import static io.netty.util.ByteProcessor.FIND_ASCII_SPACE;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Integer.parseInt; import static java.lang.Integer.parseInt;
/** /**
@ -538,10 +539,7 @@ public class HttpResponseStatus implements Comparable<HttpResponseStatus> {
} }
private HttpResponseStatus(int code, String reasonPhrase, boolean bytes) { private HttpResponseStatus(int code, String reasonPhrase, boolean bytes) {
if (code < 0) { checkPositiveOrZero(code, "code");
throw new IllegalArgumentException(
"code: " + code + " (expected: 0+)");
}
if (reasonPhrase == null) { if (reasonPhrase == null) {
throw new NullPointerException("reasonPhrase"); throw new NullPointerException("reasonPhrase");

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
@ -165,12 +167,8 @@ public class HttpVersion implements Comparable<HttpVersion> {
} }
} }
if (majorVersion < 0) { checkPositiveOrZero(majorVersion, "majorVersion");
throw new IllegalArgumentException("negative majorVersion"); checkPositiveOrZero(minorVersion, "minorVersion");
}
if (minorVersion < 0) {
throw new IllegalArgumentException("negative minorVersion");
}
this.protocolName = protocolName; this.protocolName = protocolName;
this.majorVersion = majorVersion; this.majorVersion = majorVersion;

View File

@ -128,8 +128,7 @@ public abstract class AbstractMemoryHttpData extends AbstractHttpData {
} }
long newsize = file.length(); long newsize = file.length();
if (newsize > Integer.MAX_VALUE) { if (newsize > Integer.MAX_VALUE) {
throw new IllegalArgumentException( throw new IllegalArgumentException("File too big to be loaded in memory");
"File too big to be loaded in memory");
} }
checkSize(newsize); checkSize(newsize);
FileInputStream inputStream = new FileInputStream(file); FileInputStream inputStream = new FileInputStream(file);

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
/** /**
@ -62,10 +64,7 @@ public class DefaultSpdyGoAwayFrame implements SpdyGoAwayFrame {
@Override @Override
public SpdyGoAwayFrame setLastGoodStreamId(int lastGoodStreamId) { public SpdyGoAwayFrame setLastGoodStreamId(int lastGoodStreamId) {
if (lastGoodStreamId < 0) { checkPositiveOrZero(lastGoodStreamId, "lastGoodStreamId");
throw new IllegalArgumentException("Last-good-stream-ID"
+ " cannot be negative: " + lastGoodStreamId);
}
this.lastGoodStreamId = lastGoodStreamId; this.lastGoodStreamId = lastGoodStreamId;
return this; return this;
} }

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/** /**
* The default {@link SpdyStreamFrame} implementation. * The default {@link SpdyStreamFrame} implementation.
*/ */
@ -39,10 +41,7 @@ public abstract class DefaultSpdyStreamFrame implements SpdyStreamFrame {
@Override @Override
public SpdyStreamFrame setStreamId(int streamId) { public SpdyStreamFrame setStreamId(int streamId) {
if (streamId <= 0) { checkPositive(streamId, "streamId");
throw new IllegalArgumentException(
"Stream-ID must be positive: " + streamId);
}
this.streamId = streamId; this.streamId = streamId;
return this; return this;
} }

View File

@ -20,8 +20,7 @@ import io.netty.util.internal.StringUtil;
/** /**
* The default {@link SpdySynReplyFrame} implementation. * The default {@link SpdySynReplyFrame} implementation.
*/ */
public class DefaultSpdySynReplyFrame extends DefaultSpdyHeadersFrame public class DefaultSpdySynReplyFrame extends DefaultSpdyHeadersFrame implements SpdySynReplyFrame {
implements SpdySynReplyFrame {
/** /**
* Creates a new instance. * Creates a new instance.

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
/** /**
@ -77,11 +79,7 @@ public class DefaultSpdySynStreamFrame extends DefaultSpdyHeadersFrame
@Override @Override
public SpdySynStreamFrame setAssociatedStreamId(int associatedStreamId) { public SpdySynStreamFrame setAssociatedStreamId(int associatedStreamId) {
if (associatedStreamId < 0) { checkPositiveOrZero(associatedStreamId, "associatedStreamId");
throw new IllegalArgumentException(
"Associated-To-Stream-ID cannot be negative: " +
associatedStreamId);
}
this.associatedStreamId = associatedStreamId; this.associatedStreamId = associatedStreamId;
return this; return this;
} }

View File

@ -15,6 +15,9 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
/** /**
@ -43,10 +46,7 @@ public class DefaultSpdyWindowUpdateFrame implements SpdyWindowUpdateFrame {
@Override @Override
public SpdyWindowUpdateFrame setStreamId(int streamId) { public SpdyWindowUpdateFrame setStreamId(int streamId) {
if (streamId < 0) { checkPositiveOrZero(streamId, "streamId");
throw new IllegalArgumentException(
"Stream-ID cannot be negative: " + streamId);
}
this.streamId = streamId; this.streamId = streamId;
return this; return this;
} }
@ -58,11 +58,7 @@ public class DefaultSpdyWindowUpdateFrame implements SpdyWindowUpdateFrame {
@Override @Override
public SpdyWindowUpdateFrame setDeltaWindowSize(int deltaWindowSize) { public SpdyWindowUpdateFrame setDeltaWindowSize(int deltaWindowSize) {
if (deltaWindowSize <= 0) { checkPositive(deltaWindowSize, "deltaWindowSize");
throw new IllegalArgumentException(
"Delta-Window-Size must be positive: " +
deltaWindowSize);
}
this.deltaWindowSize = deltaWindowSize; this.deltaWindowSize = deltaWindowSize;
return this; return this;
} }

View File

@ -38,6 +38,8 @@ import static io.netty.handler.codec.spdy.SpdyCodecUtil.getSignedInt;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedInt; import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedInt;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedMedium; import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedMedium;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedShort; import static io.netty.handler.codec.spdy.SpdyCodecUtil.getUnsignedShort;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -95,10 +97,7 @@ public class SpdyFrameDecoder {
if (delegate == null) { if (delegate == null) {
throw new NullPointerException("delegate"); throw new NullPointerException("delegate");
} }
if (maxChunkSize <= 0) { checkPositive(maxChunkSize, "maxChunkSize");
throw new IllegalArgumentException(
"maxChunkSize must be a positive integer: " + maxChunkSize);
}
this.spdyVersion = spdyVersion.getVersion(); this.spdyVersion = spdyVersion.getVersion();
this.delegate = delegate; this.delegate = delegate;
this.maxChunkSize = maxChunkSize; this.maxChunkSize = maxChunkSize;

View File

@ -38,6 +38,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import static io.netty.handler.codec.spdy.SpdyHeaders.HttpNames.*; import static io.netty.handler.codec.spdy.SpdyHeaders.HttpNames.*;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/** /**
* Decodes {@link SpdySynStreamFrame}s, {@link SpdySynReplyFrame}s, * Decodes {@link SpdySynStreamFrame}s, {@link SpdySynReplyFrame}s,
@ -103,10 +104,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
if (version == null) { if (version == null) {
throw new NullPointerException("version"); throw new NullPointerException("version");
} }
if (maxContentLength <= 0) { checkPositive(maxContentLength, "maxContentLength");
throw new IllegalArgumentException(
"maxContentLength must be a positive integer: " + maxContentLength);
}
spdyVersion = version.getVersion(); spdyVersion = version.getVersion();
this.maxContentLength = maxContentLength; this.maxContentLength = maxContentLength;
this.messageMap = messageMap; this.messageMap = messageMap;

View File

@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_SESSION_STREAM_ID; import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_SESSION_STREAM_ID;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.isServerId; import static io.netty.handler.codec.spdy.SpdyCodecUtil.isServerId;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/** /**
* Manages streams within a SPDY session. * Manages streams within a SPDY session.
@ -77,9 +78,7 @@ public class SpdySessionHandler extends ChannelDuplexHandler {
} }
public void setSessionReceiveWindowSize(int sessionReceiveWindowSize) { public void setSessionReceiveWindowSize(int sessionReceiveWindowSize) {
if (sessionReceiveWindowSize < 0) { checkPositiveOrZero(sessionReceiveWindowSize, "sessionReceiveWindowSize");
throw new IllegalArgumentException("sessionReceiveWindowSize");
}
// This will not send a window update frame immediately. // This will not send a window update frame immediately.
// If this value increases the allowed receive window size, // If this value increases the allowed receive window size,
// a WINDOW_UPDATE frame will be sent when only half of the // a WINDOW_UPDATE frame will be sent when only half of the

View File

@ -30,6 +30,7 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGH
import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR; import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError; import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.MAX_VALUE;
import static java.lang.Math.min; import static java.lang.Math.min;
@ -543,9 +544,7 @@ public class DefaultHttp2ConnectionEncoder implements Http2ConnectionEncoder {
FlowControlledBase(final Http2Stream stream, int padding, boolean endOfStream, FlowControlledBase(final Http2Stream stream, int padding, boolean endOfStream,
final ChannelPromise promise) { final ChannelPromise promise) {
if (padding < 0) { checkPositiveOrZero(padding, "padding");
throw new IllegalArgumentException("padding must be >= 0");
}
this.padding = padding; this.padding = padding;
this.endOfStream = endOfStream; this.endOfStream = endOfStream;
this.stream = stream; this.stream = stream;

View File

@ -61,6 +61,8 @@ import static io.netty.handler.codec.http2.Http2FrameTypes.RST_STREAM;
import static io.netty.handler.codec.http2.Http2FrameTypes.SETTINGS; import static io.netty.handler.codec.http2.Http2FrameTypes.SETTINGS;
import static io.netty.handler.codec.http2.Http2FrameTypes.WINDOW_UPDATE; import static io.netty.handler.codec.http2.Http2FrameTypes.WINDOW_UPDATE;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
@ -614,15 +616,11 @@ public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSize
} }
private static void verifyStreamId(int streamId, String argumentName) { private static void verifyStreamId(int streamId, String argumentName) {
if (streamId <= 0) { checkPositive(streamId, "streamId");
throw new IllegalArgumentException(argumentName + " must be > 0");
}
} }
private static void verifyStreamOrConnectionId(int streamId, String argumentName) { private static void verifyStreamOrConnectionId(int streamId, String argumentName) {
if (streamId < 0) { checkPositiveOrZero(streamId, "streamId");
throw new IllegalArgumentException(argumentName + " must be >= 0");
}
} }
private static void verifyWeight(short weight) { private static void verifyWeight(short weight) {
@ -638,9 +636,7 @@ public class DefaultHttp2FrameWriter implements Http2FrameWriter, Http2FrameSize
} }
private static void verifyWindowSizeIncrement(int windowSizeIncrement) { private static void verifyWindowSizeIncrement(int windowSizeIncrement) {
if (windowSizeIncrement < 0) { checkPositiveOrZero(windowSizeIncrement, "windowSizeIncrement");
throw new IllegalArgumentException("WindowSizeIncrement must be >= 0");
}
} }
private static void verifyPingPayload(ByteBuf data) { private static void verifyPingPayload(ByteBuf data) {

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.http2; package io.netty.handler.codec.http2;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.DefaultByteBufHolder; import io.netty.buffer.DefaultByteBufHolder;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -98,9 +100,7 @@ public final class DefaultHttp2GoAwayFrame extends DefaultByteBufHolder implemen
@Override @Override
public Http2GoAwayFrame setExtraStreamIds(int extraStreamIds) { public Http2GoAwayFrame setExtraStreamIds(int extraStreamIds) {
if (extraStreamIds < 0) { checkPositiveOrZero(extraStreamIds, "extraStreamIds");
throw new IllegalArgumentException("extraStreamIds must be non-negative");
}
this.extraStreamIds = extraStreamIds; this.extraStreamIds = extraStreamIds;
return this; return this;
} }

View File

@ -24,6 +24,7 @@ import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError; import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.handler.codec.http2.Http2Exception.streamError; import static io.netty.handler.codec.http2.Http2Exception.streamError;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -173,9 +174,7 @@ public class DefaultHttp2LocalFlowController implements Http2LocalFlowController
@Override @Override
public boolean consumeBytes(Http2Stream stream, int numBytes) throws Http2Exception { public boolean consumeBytes(Http2Stream stream, int numBytes) throws Http2Exception {
assert ctx != null && ctx.executor().inEventLoop(); assert ctx != null && ctx.executor().inEventLoop();
if (numBytes < 0) { checkPositiveOrZero(numBytes, "numBytes");
throw new IllegalArgumentException("numBytes must not be negative");
}
if (numBytes == 0) { if (numBytes == 0) {
return false; return false;
} }

View File

@ -31,6 +31,7 @@ import static io.netty.handler.codec.http2.Http2Error.STREAM_CLOSED;
import static io.netty.handler.codec.http2.Http2Exception.streamError; import static io.netty.handler.codec.http2.Http2Exception.streamError;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_LOCAL; import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_LOCAL;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
@ -635,9 +636,7 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll
} }
void initialWindowSize(int newWindowSize) throws Http2Exception { void initialWindowSize(int newWindowSize) throws Http2Exception {
if (newWindowSize < 0) { checkPositiveOrZero(newWindowSize, "newWindowSize");
throw new IllegalArgumentException("Invalid initial window size: " + newWindowSize);
}
final int delta = newWindowSize - initialWindowSize; final int delta = newWindowSize - initialWindowSize;
initialWindowSize = newWindowSize; initialWindowSize = newWindowSize;

View File

@ -33,6 +33,7 @@ import static io.netty.handler.codec.http.HttpHeaderValues.X_GZIP;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR; import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.streamError; import static io.netty.handler.codec.http2.Http2Exception.streamError;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/** /**
* A HTTP2 frame listener that will decompress data frames according to the {@code content-encoding} header for each * A HTTP2 frame listener that will decompress data frames according to the {@code content-encoding} header for each
@ -398,9 +399,7 @@ public class DelegatingDecompressorFrameListener extends Http2FrameListenerDecor
* @return The number of pre-decompressed bytes that have been consumed. * @return The number of pre-decompressed bytes that have been consumed.
*/ */
int consumeBytes(int streamId, int decompressedBytes) throws Http2Exception { int consumeBytes(int streamId, int decompressedBytes) throws Http2Exception {
if (decompressedBytes < 0) { checkPositiveOrZero(decompressedBytes, "decompressedBytes");
throw new IllegalArgumentException("decompressedBytes must not be negative: " + decompressedBytes);
}
if (decompressed - decompressedBytes < 0) { if (decompressed - decompressedBytes < 0) {
throw streamError(streamId, INTERNAL_ERROR, throw streamError(streamId, INTERNAL_ERROR,
"Attempting to return too many bytes for stream %d. decompressed: %d " + "Attempting to return too many bytes for stream %d. decompressed: %d " +

View File

@ -24,6 +24,7 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.streamableBytes;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR; import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError; import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.util.internal.ObjectUtil.checkNotNull; import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
@ -72,9 +73,7 @@ public final class UniformStreamByteDistributor implements StreamByteDistributor
* Must be > 0. * Must be > 0.
*/ */
public void minAllocationChunk(int minAllocationChunk) { public void minAllocationChunk(int minAllocationChunk) {
if (minAllocationChunk <= 0) { checkPositive(minAllocationChunk, "minAllocationChunk");
throw new IllegalArgumentException("minAllocationChunk must be > 0");
}
this.minAllocationChunk = minAllocationChunk; this.minAllocationChunk = minAllocationChunk;
} }

View File

@ -37,6 +37,8 @@ import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_PRIORITY_WEIGH
import static io.netty.handler.codec.http2.Http2CodecUtil.streamableBytes; import static io.netty.handler.codec.http2.Http2CodecUtil.streamableBytes;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR; import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError; import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Integer.MAX_VALUE; import static java.lang.Integer.MAX_VALUE;
import static java.lang.Math.max; import static java.lang.Math.max;
import static java.lang.Math.min; import static java.lang.Math.min;
@ -96,9 +98,8 @@ public final class WeightedFairQueueByteDistributor implements StreamByteDistrib
} }
public WeightedFairQueueByteDistributor(Http2Connection connection, int maxStateOnlySize) { public WeightedFairQueueByteDistributor(Http2Connection connection, int maxStateOnlySize) {
if (maxStateOnlySize < 0) { checkPositiveOrZero(maxStateOnlySize, "maxStateOnlySize");
throw new IllegalArgumentException("maxStateOnlySize: " + maxStateOnlySize + " (expected: >0)"); if (maxStateOnlySize == 0) {
} else if (maxStateOnlySize == 0) {
stateOnlyMap = IntCollections.emptyMap(); stateOnlyMap = IntCollections.emptyMap();
stateOnlyRemovalQueue = EmptyPriorityQueue.instance(); stateOnlyRemovalQueue = EmptyPriorityQueue.instance();
} else { } else {
@ -281,9 +282,7 @@ public final class WeightedFairQueueByteDistributor implements StreamByteDistrib
* @param allocationQuantum the amount of bytes that will be allocated to each stream. Must be &gt; 0. * @param allocationQuantum the amount of bytes that will be allocated to each stream. Must be &gt; 0.
*/ */
public void allocationQuantum(int allocationQuantum) { public void allocationQuantum(int allocationQuantum) {
if (allocationQuantum <= 0) { checkPositive(allocationQuantum, "allocationQuantum");
throw new IllegalArgumentException("allocationQuantum must be > 0");
}
this.allocationQuantum = allocationQuantum; this.allocationQuantum = allocationQuantum;
} }

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.handler.codec.memcache.binary; package io.netty.handler.codec.memcache.binary;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -59,9 +61,7 @@ public abstract class AbstractBinaryMemcacheDecoder<M extends BinaryMemcacheMess
* @param chunkSize the maximum chunk size of the payload. * @param chunkSize the maximum chunk size of the payload.
*/ */
protected AbstractBinaryMemcacheDecoder(int chunkSize) { protected AbstractBinaryMemcacheDecoder(int chunkSize) {
if (chunkSize < 0) { checkPositiveOrZero(chunkSize, "chunkSize");
throw new IllegalArgumentException("chunkSize must be a positive integer: " + chunkSize);
}
this.chunkSize = chunkSize; this.chunkSize = chunkSize;
} }

View File

@ -30,6 +30,7 @@ import io.netty.util.internal.AppendableCharSequence;
import static io.netty.buffer.ByteBufUtil.indexOf; import static io.netty.buffer.ByteBufUtil.indexOf;
import static io.netty.buffer.ByteBufUtil.readBytes; import static io.netty.buffer.ByteBufUtil.readBytes;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/** /**
* Decodes {@link ByteBuf}s into {@link StompHeadersSubframe}s and * Decodes {@link ByteBuf}s into {@link StompHeadersSubframe}s and
@ -90,16 +91,8 @@ public class StompSubframeDecoder extends ReplayingDecoder<State> {
public StompSubframeDecoder(int maxLineLength, int maxChunkSize, boolean validateHeaders) { public StompSubframeDecoder(int maxLineLength, int maxChunkSize, boolean validateHeaders) {
super(State.SKIP_CONTROL_CHARACTERS); super(State.SKIP_CONTROL_CHARACTERS);
if (maxLineLength <= 0) { checkPositive(maxLineLength, "maxLineLength");
throw new IllegalArgumentException( checkPositive(maxChunkSize, "maxChunkSize");
"maxLineLength must be a positive integer: " +
maxLineLength);
}
if (maxChunkSize <= 0) {
throw new IllegalArgumentException(
"maxChunkSize must be a positive integer: " +
maxChunkSize);
}
this.maxChunkSize = maxChunkSize; this.maxChunkSize = maxChunkSize;
this.maxLineLength = maxLineLength; this.maxLineLength = maxLineLength;
this.validateHeaders = validateHeaders; this.validateHeaders = validateHeaders;