use checkPositive/checkPositiveOrZero (#8803)

Motivation:

We have a utility method to check for > 0 and >0 arguments. We should use it.

Modification:

use checkPositive/checkPositiveOrZero instead of if statement.

Result:

Re-use utility method.
This commit is contained in:
田欧 2019-01-31 16:06:59 +08:00 committed by Norman Maurer
parent fe4a59011a
commit a33200ca38
32 changed files with 114 additions and 199 deletions

View File

@ -38,6 +38,7 @@ import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.Charset;
import static io.netty.util.internal.MathUtil.isOutOfBounds;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* A skeletal implementation of a buffer.
@ -73,9 +74,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
private int maxCapacity;
protected AbstractByteBuf(int maxCapacity) {
if (maxCapacity < 0) {
throw new IllegalArgumentException("maxCapacity: " + maxCapacity + " (expected: >= 0)");
}
checkPositiveOrZero(maxCapacity, "maxCapacity");
this.maxCapacity = maxCapacity;
}
@ -271,10 +270,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public ByteBuf ensureWritable(int minWritableBytes) {
if (minWritableBytes < 0) {
throw new IllegalArgumentException(String.format(
"minWritableBytes: %d (expected: >= 0)", minWritableBytes));
}
checkPositiveOrZero(minWritableBytes, "minWritableBytes");
ensureWritable0(minWritableBytes);
return this;
}
@ -302,10 +298,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override
public int ensureWritable(int minWritableBytes, boolean force) {
ensureAccessible();
if (minWritableBytes < 0) {
throw new IllegalArgumentException(String.format(
"minWritableBytes: %d (expected: >= 0)", minWritableBytes));
}
checkPositiveOrZero(minWritableBytes, "minWritableBytes");
if (minWritableBytes <= writableBytes()) {
return 0;
@ -1414,9 +1407,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
* than the specified value.
*/
protected final void checkReadableBytes(int minimumReadableBytes) {
if (minimumReadableBytes < 0) {
throw new IllegalArgumentException("minimumReadableBytes: " + minimumReadableBytes + " (expected: >= 0)");
}
checkPositiveOrZero(minimumReadableBytes, "minimumReadableBytes");
checkReadableBytes0(minimumReadableBytes);
}

View File

@ -16,6 +16,8 @@
package io.netty.buffer;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.ResourceLeakDetector;
import io.netty.util.ResourceLeakTracker;
import io.netty.util.internal.PlatformDependent;
@ -222,9 +224,7 @@ public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
}
private static void validate(int initialCapacity, int maxCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("initialCapacity: " + initialCapacity + " (expected: 0+)");
}
checkPositiveOrZero(initialCapacity, "initialCapacity");
if (initialCapacity > maxCapacity) {
throw new IllegalArgumentException(String.format(
"initialCapacity: %d (expected: not greater than maxCapacity(%d)",
@ -249,9 +249,7 @@ public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
@Override
public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
if (minNewCapacity < 0) {
throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)");
}
checkPositiveOrZero(minNewCapacity, "minNewCapacity");
if (minNewCapacity > maxCapacity) {
throw new IllegalArgumentException(String.format(
"minNewCapacity: %d (expected: not greater than maxCapacity(%d)",

View File

@ -43,6 +43,7 @@ import java.util.Locale;
import static io.netty.util.internal.MathUtil.isOutOfBounds;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static io.netty.util.internal.StringUtil.NEWLINE;
import static io.netty.util.internal.StringUtil.isSurrogate;
@ -1000,9 +1001,7 @@ public final class ByteBufUtil {
}
private static String hexDump(ByteBuf buffer, int fromIndex, int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
}
checkPositiveOrZero(length, "length");
if (length == 0) {
return "";
}
@ -1022,9 +1021,7 @@ public final class ByteBufUtil {
}
private static String hexDump(byte[] array, int fromIndex, int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
}
checkPositiveOrZero(length, "length");
if (length == 0) {
return "";
}

View File

@ -16,6 +16,8 @@
package io.netty.buffer;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.ByteProcessor;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
@ -223,9 +225,7 @@ public final class EmptyByteBuf extends ByteBuf {
@Override
public ByteBuf ensureWritable(int minWritableBytes) {
if (minWritableBytes < 0) {
throw new IllegalArgumentException("minWritableBytes: " + minWritableBytes + " (expected: >= 0)");
}
checkPositiveOrZero(minWritableBytes, "minWritableBytes");
if (minWritableBytes != 0) {
throw new IndexOutOfBoundsException();
}
@ -234,9 +234,7 @@ public final class EmptyByteBuf extends ByteBuf {
@Override
public int ensureWritable(int minWritableBytes, boolean force) {
if (minWritableBytes < 0) {
throw new IllegalArgumentException("minWritableBytes: " + minWritableBytes + " (expected: >= 0)");
}
checkPositiveOrZero(minWritableBytes, "minWritableBytes");
if (minWritableBytes == 0) {
return 0;
@ -1048,9 +1046,7 @@ public final class EmptyByteBuf extends ByteBuf {
}
private ByteBuf checkIndex(int index, int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
}
checkPositiveOrZero(length, "length");
if (index != 0 || length != 0) {
throw new IndexOutOfBoundsException();
}
@ -1058,9 +1054,7 @@ public final class EmptyByteBuf extends ByteBuf {
}
private ByteBuf checkLength(int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length + " (expected: >= 0)");
}
checkPositiveOrZero(length, "length");
if (length != 0) {
throw new IndexOutOfBoundsException();
}

View File

@ -26,6 +26,7 @@ import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.max;
abstract class PoolArena<T> implements PoolArenaMetric {
@ -330,9 +331,7 @@ abstract class PoolArena<T> implements PoolArenaMetric {
}
int normalizeCapacity(int reqCapacity) {
if (reqCapacity < 0) {
throw new IllegalArgumentException("capacity: " + reqCapacity + " (expected: 0+)");
}
checkPositiveOrZero(reqCapacity, "reqCapacity");
if (reqCapacity >= chunkSize) {
return directMemoryCacheAlignment == 0 ? reqCapacity : alignCapacity(reqCapacity);

View File

@ -17,6 +17,8 @@
package io.netty.buffer;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.PoolArena.SizeClass;
import io.netty.util.Recycler;
import io.netty.util.Recycler.Handle;
@ -65,10 +67,7 @@ final class PoolThreadCache {
PoolThreadCache(PoolArena<byte[]> heapArena, PoolArena<ByteBuffer> directArena,
int tinyCacheSize, int smallCacheSize, int normalCacheSize,
int maxCachedBufferCapacity, int freeSweepAllocationThreshold) {
if (maxCachedBufferCapacity < 0) {
throw new IllegalArgumentException("maxCachedBufferCapacity: "
+ maxCachedBufferCapacity + " (expected: >= 0)");
}
checkPositiveOrZero(maxCachedBufferCapacity, "maxCachedBufferCapacity");
this.freeSweepAllocationThreshold = freeSweepAllocationThreshold;
this.heapArena = heapArena;
this.directArena = directArena;

View File

@ -16,6 +16,8 @@
package io.netty.buffer;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.NettyRuntime;
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.concurrent.FastThreadLocalThread;
@ -215,17 +217,10 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator implements
this.normalCacheSize = normalCacheSize;
chunkSize = validateAndCalculateChunkSize(pageSize, maxOrder);
if (nHeapArena < 0) {
throw new IllegalArgumentException("nHeapArena: " + nHeapArena + " (expected: >= 0)");
}
if (nDirectArena < 0) {
throw new IllegalArgumentException("nDirectArea: " + nDirectArena + " (expected: >= 0)");
}
checkPositiveOrZero(nHeapArena, "nHeapArena");
checkPositiveOrZero(nDirectArena, "nDirectArena");
if (directMemoryCacheAlignment < 0) {
throw new IllegalArgumentException("directMemoryCacheAlignment: "
+ directMemoryCacheAlignment + " (expected: >= 0)");
}
checkPositiveOrZero(directMemoryCacheAlignment, "directMemoryCacheAlignment");
if (directMemoryCacheAlignment > 0 && !isDirectMemoryCacheAlignmentSupported()) {
throw new IllegalArgumentException("directMemoryCacheAlignment is not supported");
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.buffer;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.PlatformDependent;
import java.io.IOException;
@ -52,12 +54,8 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
if (alloc == null) {
throw new NullPointerException("alloc");
}
if (initialCapacity < 0) {
throw new IllegalArgumentException("initialCapacity: " + initialCapacity);
}
if (maxCapacity < 0) {
throw new IllegalArgumentException("maxCapacity: " + maxCapacity);
}
checkPositiveOrZero(initialCapacity, "initialCapacity");
checkPositiveOrZero(maxCapacity, "maxCapacity");
if (initialCapacity > maxCapacity) {
throw new IllegalArgumentException(String.format(
"initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));

View File

@ -15,6 +15,8 @@
*/
package io.netty.buffer;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.internal.PlatformDependent;
import java.io.IOException;
@ -53,12 +55,8 @@ public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf
if (alloc == null) {
throw new NullPointerException("alloc");
}
if (initialCapacity < 0) {
throw new IllegalArgumentException("initialCapacity: " + initialCapacity);
}
if (maxCapacity < 0) {
throw new IllegalArgumentException("maxCapacity: " + maxCapacity);
}
checkPositiveOrZero(initialCapacity, "initialCapacity");
checkPositiveOrZero(maxCapacity, "maxCapacity");
if (initialCapacity > maxCapacity) {
throw new IllegalArgumentException(String.format(
"initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
@ -202,9 +204,7 @@ public abstract class ByteToMessageDecoder extends ChannelInboundHandlerAdapter
* The default is {@code 16}.
*/
public void setDiscardAfterReads(int discardAfterReads) {
if (discardAfterReads <= 0) {
throw new IllegalArgumentException("discardAfterReads must be > 0");
}
checkPositive(discardAfterReads, "discardAfterReads");
this.discardAfterReads = discardAfterReads;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
@ -346,10 +348,6 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
}
private static void validateMaxFrameLength(int maxFrameLength) {
if (maxFrameLength <= 0) {
throw new IllegalArgumentException(
"maxFrameLength must be a positive integer: " +
maxFrameLength);
}
checkPositive(maxFrameLength, "maxFrameLength");
}
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
@ -46,10 +48,7 @@ public class FixedLengthFrameDecoder extends ByteToMessageDecoder {
* @param frameLength the length of the frame
*/
public FixedLengthFrameDecoder(int frameLength) {
if (frameLength <= 0) {
throw new IllegalArgumentException(
"frameLength must be a positive integer: " + frameLength);
}
checkPositive(frameLength, "frameLength");
this.frameLength = frameLength;
}

View File

@ -15,6 +15,9 @@
*/
package io.netty.handler.codec;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import java.nio.ByteOrder;
import java.util.List;
@ -302,23 +305,11 @@ public class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder {
throw new NullPointerException("byteOrder");
}
if (maxFrameLength <= 0) {
throw new IllegalArgumentException(
"maxFrameLength must be a positive integer: " +
maxFrameLength);
}
checkPositive(maxFrameLength, "maxFrameLength");
if (lengthFieldOffset < 0) {
throw new IllegalArgumentException(
"lengthFieldOffset must be a non-negative integer: " +
lengthFieldOffset);
}
checkPositiveOrZero(lengthFieldOffset, "lengthFieldOffset");
if (initialBytesToStrip < 0) {
throw new IllegalArgumentException(
"initialBytesToStrip must be a non-negative integer: " +
initialBytesToStrip);
}
checkPositiveOrZero(initialBytesToStrip, "initialBytesToStrip");
if (lengthFieldOffset > maxFrameLength - lengthFieldLength) {
throw new IllegalArgumentException(

View File

@ -15,6 +15,8 @@
*/
package io.netty.handler.codec;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
@ -163,10 +165,7 @@ public class LengthFieldPrepender extends MessageToMessageEncoder<ByteBuf> {
length += lengthFieldLength;
}
if (length < 0) {
throw new IllegalArgumentException(
"Adjusted frame length (" + length + ") is less than zero");
}
checkPositiveOrZero(length, "length");
switch (lengthFieldLength) {
case 1:

View File

@ -28,6 +28,7 @@ import io.netty.util.ReferenceCountUtil;
import java.util.List;
import static io.netty.buffer.Unpooled.EMPTY_BUFFER;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* An abstract {@link ChannelHandler} that aggregates a series of message objects into a single aggregated message.
@ -81,9 +82,7 @@ public abstract class MessageAggregator<I, S, C extends ByteBufHolder, O extends
}
private static void validateMaxContentLength(int maxContentLength) {
if (maxContentLength < 0) {
throw new IllegalArgumentException("maxContentLength: " + maxContentLength + " (expected: >= 0)");
}
checkPositiveOrZero(maxContentLength, "maxContentLength");
}
@Override

View File

@ -16,6 +16,8 @@
package io.netty.testsuite.http2;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
@ -59,9 +61,7 @@ public class Http2ServerInitializer extends ChannelInitializer<SocketChannel> {
}
Http2ServerInitializer(int maxHttpContentLength) {
if (maxHttpContentLength < 0) {
throw new IllegalArgumentException("maxHttpContentLength (expected >= 0): " + maxHttpContentLength);
}
checkPositiveOrZero(maxHttpContentLength, "maxHttpContentLength");
this.maxHttpContentLength = maxHttpContentLength;
}

View File

@ -54,6 +54,7 @@ import static io.netty.channel.internal.ChannelUtils.MAX_BYTES_PER_GATHERING_WRI
import static io.netty.channel.internal.ChannelUtils.WRITE_STATUS_SNDBUF_FULL;
import static io.netty.channel.unix.FileDescriptor.pipe;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
public abstract class AbstractEpollStreamChannel extends AbstractEpollChannel implements DuplexChannel {
private static final ChannelMetadata METADATA = new ChannelMetadata(false, 16);
@ -163,9 +164,7 @@ public abstract class AbstractEpollStreamChannel extends AbstractEpollChannel im
if (ch.eventLoop() != eventLoop()) {
throw new IllegalArgumentException("EventLoops are not the same.");
}
if (len < 0) {
throw new IllegalArgumentException("len: " + len + " (expected: >= 0)");
}
checkPositiveOrZero(len, "len");
if (ch.config().getEpollMode() != EpollMode.LEVEL_TRIGGERED
|| config().getEpollMode() != EpollMode.LEVEL_TRIGGERED) {
throw new IllegalStateException("spliceTo() supported only when using " + EpollMode.LEVEL_TRIGGERED);
@ -214,12 +213,8 @@ public abstract class AbstractEpollStreamChannel extends AbstractEpollChannel im
*/
public final ChannelFuture spliceTo(final FileDescriptor ch, final int offset, final int len,
final ChannelPromise promise) {
if (len < 0) {
throw new IllegalArgumentException("len: " + len + " (expected: >= 0)");
}
if (offset < 0) {
throw new IllegalArgumentException("offset must be >= 0 but was " + offset);
}
checkPositiveOrZero(len, "len");
checkPositiveOrZero(offset, "offser");
if (config().getEpollMode() != EpollMode.LEVEL_TRIGGERED) {
throw new IllegalStateException("spliceTo() supported only when using " + EpollMode.LEVEL_TRIGGERED);
}

View File

@ -30,6 +30,7 @@ import java.util.Map;
import static io.netty.channel.ChannelOption.SO_BACKLOG;
import static io.netty.channel.ChannelOption.SO_RCVBUF;
import static io.netty.channel.ChannelOption.SO_REUSEADDR;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
public class EpollServerChannelConfig extends EpollChannelConfig implements ServerSocketChannelConfig {
private volatile int backlog = NetUtil.SOMAXCONN;
@ -120,9 +121,7 @@ public class EpollServerChannelConfig extends EpollChannelConfig implements Serv
}
public EpollServerChannelConfig setBacklog(int backlog) {
if (backlog < 0) {
throw new IllegalArgumentException("backlog: " + backlog);
}
checkPositiveOrZero(backlog, "backlog");
this.backlog = backlog;
return this;
}
@ -146,9 +145,7 @@ public class EpollServerChannelConfig extends EpollChannelConfig implements Serv
* @see <a href="https://tools.ietf.org/html/rfc7413">RFC 7413 TCP FastOpen</a>
*/
public EpollServerChannelConfig setTcpFastopen(int pendingFastOpenRequestsThreshold) {
if (this.pendingFastOpenRequestsThreshold < 0) {
throw new IllegalArgumentException("pendingFastOpenRequestsThreshold: " + pendingFastOpenRequestsThreshold);
}
checkPositiveOrZero(this.pendingFastOpenRequestsThreshold, "pendingFastOpenRequestsThreshold");
this.pendingFastOpenRequestsThreshold = pendingFastOpenRequestsThreshold;
return this;
}

View File

@ -31,6 +31,7 @@ import java.util.Map;
import static io.netty.channel.ChannelOption.SO_BACKLOG;
import static io.netty.channel.ChannelOption.SO_RCVBUF;
import static io.netty.channel.ChannelOption.SO_REUSEADDR;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
@UnstableApi
public class KQueueServerChannelConfig extends KQueueChannelConfig implements ServerSocketChannelConfig {
@ -116,9 +117,7 @@ public class KQueueServerChannelConfig extends KQueueChannelConfig implements Se
}
public KQueueServerChannelConfig setBacklog(int backlog) {
if (backlog < 0) {
throw new IllegalArgumentException("backlog: " + backlog);
}
checkPositiveOrZero(backlog, "backlog");
this.backlog = backlog;
return this;
}

View File

@ -27,6 +27,7 @@ import static io.netty.channel.unix.Errors.ioResult;
import static io.netty.channel.unix.Errors.newIOException;
import static io.netty.channel.unix.Limits.IOV_MAX;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Math.min;
/**
@ -82,9 +83,7 @@ public class FileDescriptor {
final int fd;
public FileDescriptor(int fd) {
if (fd < 0) {
throw new IllegalArgumentException("fd must be >= 0");
}
checkPositiveOrZero(fd, "fd");
this.fd = fd;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel.sctp;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import com.sun.nio.sctp.SctpServerChannel;
import com.sun.nio.sctp.SctpStandardSocketOptions;
import io.netty.buffer.ByteBufAllocator;
@ -152,9 +154,7 @@ public class DefaultSctpServerChannelConfig extends DefaultChannelConfig impleme
@Override
public SctpServerChannelConfig setBacklog(int backlog) {
if (backlog < 0) {
throw new IllegalArgumentException("backlog: " + backlog);
}
checkPositiveOrZero(backlog, "backlog");
this.backlog = backlog;
return this;
}

View File

@ -18,6 +18,7 @@ package io.netty.channel;
import java.util.ArrayList;
import java.util.List;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static java.lang.Math.max;
import static java.lang.Math.min;
@ -163,9 +164,7 @@ public class AdaptiveRecvByteBufAllocator extends DefaultMaxMessagesRecvByteBufA
* @param maximum the inclusive upper bound of the expected buffer size
*/
public AdaptiveRecvByteBufAllocator(int minimum, int initial, int maximum) {
if (minimum <= 0) {
throw new IllegalArgumentException("minimum: " + minimum);
}
checkPositive(minimum, "minimum");
if (initial < minimum) {
throw new IllegalArgumentException("initial: " + initial);
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import java.util.ArrayDeque;
import java.util.Queue;
@ -64,9 +66,7 @@ public final class ChannelFlushPromiseNotifier {
if (promise == null) {
throw new NullPointerException("promise");
}
if (pendingDataSize < 0) {
throw new IllegalArgumentException("pendingDataSize must be >= 0 but was " + pendingDataSize);
}
checkPositiveOrZero(pendingDataSize, "pendingDataSize");
long checkpoint = writeCounter + pendingDataSize;
if (promise instanceof FlushCheckpoint) {
FlushCheckpoint cp = (FlushCheckpoint) promise;
@ -81,9 +81,7 @@ public final class ChannelFlushPromiseNotifier {
* Increase the current write counter by the given delta
*/
public ChannelFlushPromiseNotifier increaseWriteCounter(long delta) {
if (delta < 0) {
throw new IllegalArgumentException("delta must be >= 0 but was " + delta);
}
checkPositiveOrZero(delta, "delta");
writeCounter += delta;
return this;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import java.net.SocketAddress;
/**
@ -46,10 +48,7 @@ public final class ChannelMetadata {
* set for {@link MaxMessagesRecvByteBufAllocator#maxMessagesPerRead()}. Must be {@code > 0}.
*/
public ChannelMetadata(boolean hasDisconnect, int defaultMaxMessagesPerRead) {
if (defaultMaxMessagesPerRead <= 0) {
throw new IllegalArgumentException("defaultMaxMessagesPerRead: " + defaultMaxMessagesPerRead +
" (expected > 0)");
}
checkPositive(defaultMaxMessagesPerRead, "defaultMaxMessagesPerRead");
this.hasDisconnect = hasDisconnect;
this.defaultMaxMessagesPerRead = defaultMaxMessagesPerRead;
}

View File

@ -36,6 +36,8 @@ import static io.netty.channel.ChannelOption.WRITE_BUFFER_LOW_WATER_MARK;
import static io.netty.channel.ChannelOption.WRITE_BUFFER_WATER_MARK;
import static io.netty.channel.ChannelOption.WRITE_SPIN_COUNT;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* The default {@link ChannelConfig} implementation.
@ -209,10 +211,7 @@ public class DefaultChannelConfig implements ChannelConfig {
@Override
public ChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
if (connectTimeoutMillis < 0) {
throw new IllegalArgumentException(String.format(
"connectTimeoutMillis: %d (expected: >= 0)", connectTimeoutMillis));
}
checkPositiveOrZero(connectTimeoutMillis, "connectTimeoutMillis");
this.connectTimeoutMillis = connectTimeoutMillis;
return this;
}
@ -261,10 +260,7 @@ public class DefaultChannelConfig implements ChannelConfig {
@Override
public ChannelConfig setWriteSpinCount(int writeSpinCount) {
if (writeSpinCount <= 0) {
throw new IllegalArgumentException(
"writeSpinCount must be a positive integer.");
}
checkPositive(writeSpinCount, "writeSpinCount");
// Integer.MAX_VALUE is used as a special value in the channel implementations to indicate the channel cannot
// accept any more data, and results in the writeOp being set on the selector (or execute a runnable which tries
// to flush later because the writeSpinCount quantum has been exhausted). This strategy prevents additional
@ -357,10 +353,7 @@ public class DefaultChannelConfig implements ChannelConfig {
@Override
public ChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
if (writeBufferHighWaterMark < 0) {
throw new IllegalArgumentException(
"writeBufferHighWaterMark must be >= 0");
}
checkPositiveOrZero(writeBufferHighWaterMark, "writeBufferHighWaterMark");
for (;;) {
WriteBufferWaterMark waterMark = writeBufferWaterMark;
if (writeBufferHighWaterMark < waterMark.low()) {
@ -383,10 +376,7 @@ public class DefaultChannelConfig implements ChannelConfig {
@Override
public ChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
if (writeBufferLowWaterMark < 0) {
throw new IllegalArgumentException(
"writeBufferLowWaterMark must be >= 0");
}
checkPositiveOrZero(writeBufferLowWaterMark, "writeBufferLowWaterMark");
for (;;) {
WriteBufferWaterMark waterMark = writeBufferWaterMark;
if (writeBufferLowWaterMark > waterMark.high()) {

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.AbstractReferenceCounted;
import io.netty.util.IllegalReferenceCountException;
import io.netty.util.internal.logging.InternalLogger;
@ -52,12 +54,8 @@ public class DefaultFileRegion extends AbstractReferenceCounted implements FileR
if (file == null) {
throw new NullPointerException("file");
}
if (position < 0) {
throw new IllegalArgumentException("position must be >= 0 but was " + position);
}
if (count < 0) {
throw new IllegalArgumentException("count must be >= 0 but was " + count);
}
checkPositiveOrZero(position, "position");
checkPositiveOrZero(count, "count");
this.file = file;
this.position = position;
this.count = count;
@ -76,12 +74,8 @@ public class DefaultFileRegion extends AbstractReferenceCounted implements FileR
if (f == null) {
throw new NullPointerException("f");
}
if (position < 0) {
throw new IllegalArgumentException("position must be >= 0 but was " + position);
}
if (count < 0) {
throw new IllegalArgumentException("count must be >= 0 but was " + count);
}
checkPositiveOrZero(position, "position");
checkPositiveOrZero(count, "count");
this.position = position;
this.count = count;
this.f = f;

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.UncheckedBooleanSupplier;
@ -124,9 +126,7 @@ public class DefaultMaxBytesRecvByteBufAllocator implements MaxBytesRecvByteBufA
@Override
public DefaultMaxBytesRecvByteBufAllocator maxBytesPerRead(int maxBytesPerRead) {
if (maxBytesPerRead <= 0) {
throw new IllegalArgumentException("maxBytesPerRead: " + maxBytesPerRead + " (expected: > 0)");
}
checkPositive(maxBytesPerRead, "maxBytesPerRead");
// There is a dependency between this.maxBytesPerRead and this.maxBytesPerIndividualRead (a < b).
// Write operations must be synchronized, but independent read operations can just be volatile.
synchronized (this) {
@ -149,10 +149,7 @@ public class DefaultMaxBytesRecvByteBufAllocator implements MaxBytesRecvByteBufA
@Override
public DefaultMaxBytesRecvByteBufAllocator maxBytesPerIndividualRead(int maxBytesPerIndividualRead) {
if (maxBytesPerIndividualRead <= 0) {
throw new IllegalArgumentException(
"maxBytesPerIndividualRead: " + maxBytesPerIndividualRead + " (expected: > 0)");
}
checkPositive(maxBytesPerIndividualRead, "maxBytesPerIndividualRead");
// There is a dependency between this.maxBytesPerRead and this.maxBytesPerIndividualRead (a < b).
// Write operations must be synchronized, but independent read operations can just be volatile.
synchronized (this) {
@ -174,13 +171,8 @@ public class DefaultMaxBytesRecvByteBufAllocator implements MaxBytesRecvByteBufA
}
private static void checkMaxBytesPerReadPair(int maxBytesPerRead, int maxBytesPerIndividualRead) {
if (maxBytesPerRead <= 0) {
throw new IllegalArgumentException("maxBytesPerRead: " + maxBytesPerRead + " (expected: > 0)");
}
if (maxBytesPerIndividualRead <= 0) {
throw new IllegalArgumentException(
"maxBytesPerIndividualRead: " + maxBytesPerIndividualRead + " (expected: > 0)");
}
checkPositive(maxBytesPerRead, "maxBytesPerRead");
checkPositive(maxBytesPerIndividualRead, "maxBytesPerIndividualRead");
if (maxBytesPerRead < maxBytesPerIndividualRead) {
throw new IllegalArgumentException(
"maxBytesPerRead cannot be less than " +

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositive;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.UncheckedBooleanSupplier;
@ -42,9 +44,7 @@ public abstract class DefaultMaxMessagesRecvByteBufAllocator implements MaxMessa
@Override
public MaxMessagesRecvByteBufAllocator maxMessagesPerRead(int maxMessagesPerRead) {
if (maxMessagesPerRead <= 0) {
throw new IllegalArgumentException("maxMessagesPerRead: " + maxMessagesPerRead + " (expected: > 0)");
}
checkPositive(maxMessagesPerRead, "maxMessagesPerRead");
this.maxMessagesPerRead = maxMessagesPerRead;
return this;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
@ -59,9 +61,7 @@ public final class DefaultMessageSizeEstimator implements MessageSizeEstimator {
* @param unknownSize The size which is returned for unknown messages.
*/
public DefaultMessageSizeEstimator(int unknownSize) {
if (unknownSize < 0) {
throw new IllegalArgumentException("unknownSize: " + unknownSize + " (expected: >= 0)");
}
checkPositiveOrZero(unknownSize, "unknownSize");
handle = new HandleImpl(unknownSize);
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositive;
/**
* The {@link RecvByteBufAllocator} that always yields the same buffer
* size prediction. This predictor ignores the feed back from the I/O thread.
@ -41,10 +43,7 @@ public class FixedRecvByteBufAllocator extends DefaultMaxMessagesRecvByteBufAllo
* the specified buffer size.
*/
public FixedRecvByteBufAllocator(int bufferSize) {
if (bufferSize <= 0) {
throw new IllegalArgumentException(
"bufferSize must greater than 0: " + bufferSize);
}
checkPositive(bufferSize, "bufferSize");
this.bufferSize = bufferSize;
}

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* WriteBufferWaterMark is used to set low water mark and high water mark for the write buffer.
* <p>
@ -54,9 +56,7 @@ public final class WriteBufferWaterMark {
*/
WriteBufferWaterMark(int low, int high, boolean validate) {
if (validate) {
if (low < 0) {
throw new IllegalArgumentException("write buffer's low water mark must be >= 0");
}
checkPositiveOrZero(low, "low");
if (high < low) {
throw new IllegalArgumentException(
"write buffer's high water mark cannot be less than " +

View File

@ -31,6 +31,7 @@ import java.util.Map;
import static io.netty.channel.ChannelOption.SO_BACKLOG;
import static io.netty.channel.ChannelOption.SO_RCVBUF;
import static io.netty.channel.ChannelOption.SO_REUSEADDR;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
/**
* The default {@link ServerSocketChannelConfig} implementation.
@ -141,9 +142,7 @@ public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
@Override
public ServerSocketChannelConfig setBacklog(int backlog) {
if (backlog < 0) {
throw new IllegalArgumentException("backlog: " + backlog);
}
checkPositiveOrZero(backlog, "backlog");
this.backlog = backlog;
return this;
}