diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java b/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java index 69d3f1d7c1..434d25d025 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java @@ -18,6 +18,9 @@ package org.jboss.netty.channel.socket.nio; import java.nio.ByteBuffer; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.logging.InternalLogger; +import org.jboss.netty.logging.InternalLoggerFactory; +import org.jboss.netty.util.internal.SystemPropertyUtil; /** * @author The Netty Project @@ -26,7 +29,25 @@ import org.jboss.netty.buffer.ChannelBuffer; */ final class DirectBufferPool { - private static final int preallocatedBufferCapacity = 128 * 1024; + private static final InternalLogger logger = + InternalLoggerFactory.getInstance(DirectBufferPool.class); + + private static final int CAPACITY; + + static { + int val = SystemPropertyUtil.get( + "org.jboss.netty.channel.socket.nio.preallocatedBufferCapacity", + 0); + + if (val <= 0) { + val = 128 * 1024; + } else { + logger.debug( + "Using the specified preallocated buffer capacity: " + val); + } + + CAPACITY = val; + } private ByteBuffer preallocatedBuffer; @@ -45,7 +66,7 @@ final class DirectBufferPool { final ByteBuffer acquire(int size) { ByteBuffer preallocatedBuffer = this.preallocatedBuffer; if (preallocatedBuffer == null) { - if (size < preallocatedBufferCapacity) { + if (size < CAPACITY) { return preallocateAndAcquire(size); } else { return ByteBuffer.allocateDirect(size); @@ -53,7 +74,7 @@ final class DirectBufferPool { } if (preallocatedBuffer.remaining() < size) { - if (size > preallocatedBufferCapacity) { + if (size > CAPACITY) { return ByteBuffer.allocateDirect(size); } else { return preallocateAndAcquire(size); @@ -69,7 +90,7 @@ final class DirectBufferPool { private final ByteBuffer preallocateAndAcquire(int size) { ByteBuffer preallocatedBuffer = this.preallocatedBuffer = - ByteBuffer.allocateDirect(preallocatedBufferCapacity); + ByteBuffer.allocateDirect(CAPACITY); ByteBuffer x = preallocatedBuffer.duplicate(); x.limit(size); preallocatedBuffer.position(size); diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioProviderMetadata.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioProviderMetadata.java index 3bdd06075f..d6e3bba9a9 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioProviderMetadata.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioProviderMetadata.java @@ -62,8 +62,7 @@ class NioProviderMetadata { // Use the system property if possible. try { - String value = SystemPropertyUtil.get(CONSTRAINT_LEVEL_PROPERTY, "-1"); - constraintLevel = Integer.parseInt(value); + constraintLevel = SystemPropertyUtil.get(CONSTRAINT_LEVEL_PROPERTY, -1); if (constraintLevel < 0 || constraintLevel > 2) { constraintLevel = -1; } else { diff --git a/src/main/java/org/jboss/netty/util/internal/SystemPropertyUtil.java b/src/main/java/org/jboss/netty/util/internal/SystemPropertyUtil.java index 7518584f46..1c7dd383ad 100644 --- a/src/main/java/org/jboss/netty/util/internal/SystemPropertyUtil.java +++ b/src/main/java/org/jboss/netty/util/internal/SystemPropertyUtil.java @@ -15,6 +15,8 @@ */ package org.jboss.netty.util.internal; +import java.util.regex.Pattern; + /** * Accesses the system property swallowing a {@link SecurityException}. * @@ -59,6 +61,28 @@ public class SystemPropertyUtil { return value; } + /** + * Returns the value of the Java system property with the specified + * {@code key}, while falling back to the specified default value if + * the property access fails. + * + * @return the property value. + * {@code def} if there's no such property or if an access to the + * specified property is not allowed. + */ + public static int get(String key, int def) { + String value = get(key); + if (value == null) { + return def; + } + + if (Pattern.matches("-?[0-9]+", value)) { + return Integer.parseInt(value); + } else { + return def; + } + } + private SystemPropertyUtil() { // Unused }