Permalink
Browse files

Determine the default allocator from system property

- Add ByteBufAllocator.DEFAULT
- The default allocator is now 'pooled'
  • Loading branch information...
trustin committed Feb 14, 2014
1 parent bf0ae49 commit d5e897bba2635aff601cb526884164db80a9dd61
@@ -21,6 +21,8 @@
*/
public interface ByteBufAllocator {
+ ByteBufAllocator DEFAULT = ByteBufUtil.DEFAULT_ALLOCATOR;
+
/**
* Allocate a {@link ByteBuf}. If it is a direct or heap buffer
* depends on the actual implementation.
@@ -16,6 +16,9 @@
package io.netty.buffer;
import io.netty.util.CharsetUtil;
+import io.netty.util.internal.SystemPropertyUtil;
+import io.netty.util.internal.logging.InternalLogger;
+import io.netty.util.internal.logging.InternalLoggerFactory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -25,20 +28,40 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
+import java.util.Locale;
/**
* A collection of utility methods that is related with handling {@link ByteBuf}.
*/
public final class ByteBufUtil {
+ private static final InternalLogger logger = InternalLoggerFactory.getInstance(ByteBufUtil.class);
+
private static final char[] HEXDUMP_TABLE = new char[256 * 4];
+ static final ByteBufAllocator DEFAULT_ALLOCATOR;
+
static {
final char[] DIGITS = "0123456789abcdef".toCharArray();
for (int i = 0; i < 256; i ++) {
HEXDUMP_TABLE[ i << 1 ] = DIGITS[i >>> 4 & 0x0F];
HEXDUMP_TABLE[(i << 1) + 1] = DIGITS[i & 0x0F];
}
+
+ String allocType = SystemPropertyUtil.get("io.netty.allocator.type", "pooled").toLowerCase(Locale.US).trim();
+ ByteBufAllocator alloc;
+ if ("unpooled".equals(allocType)) {
+ alloc = UnpooledByteBufAllocator.DEFAULT;
+ logger.debug("-Dio.netty.allocator.type: {}", allocType);
+ } else if ("pooled".equals(allocType)) {
+ alloc = PooledByteBufAllocator.DEFAULT;
+ logger.debug("-Dio.netty.allocator.type: {}", allocType);
+ } else {
+ alloc = PooledByteBufAllocator.DEFAULT;
+ logger.debug("-Dio.netty.allocator.type: pooled (unknown: {})", allocType);
+ }
+
+ DEFAULT_ALLOCATOR = alloc;
}
/**
@@ -16,7 +16,6 @@
package io.netty.channel;
import io.netty.buffer.ByteBufAllocator;
-import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.nio.AbstractNioByteChannel;
import io.netty.channel.socket.SocketChannelConfig;
@@ -31,15 +30,14 @@
*/
public class DefaultChannelConfig implements ChannelConfig {
- private static final ByteBufAllocator DEFAULT_ALLOCATOR = UnpooledByteBufAllocator.DEFAULT;
private static final RecvByteBufAllocator DEFAULT_RCVBUF_ALLOCATOR = AdaptiveRecvByteBufAllocator.DEFAULT;
private static final MessageSizeEstimator DEFAULT_MSG_SIZE_ESTIMATOR = DefaultMessageSizeEstimator.DEFAULT;
private static final int DEFAULT_CONNECT_TIMEOUT = 30000;
protected final Channel channel;
- private volatile ByteBufAllocator allocator = DEFAULT_ALLOCATOR;
+ private volatile ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
private volatile RecvByteBufAllocator rcvBufAllocator = DEFAULT_RCVBUF_ALLOCATOR;
private volatile MessageSizeEstimator msgSizeEstimator = DEFAULT_MSG_SIZE_ESTIMATOR;

1 comment on commit d5e897b

robUx4 commented on d5e897b Dec 26, 2014

This is not running on Android. ByteBufAllocator needs the class ByteBufUtil to initialize, which itself needs the class ByteBufAllocator to initialize. This results in the following stack Trace.

io.netty.channel.ChannelException: Unable to create Channel from class class io.netty.channel.socket.nio.NioServerSocketChannel
    at io.netty.bootstrap.AbstractBootstrap$BootstrapChannelFactory.newChannel(AbstractBootstrap.java:447)
    at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:298)
    at io.netty.bootstrap.AbstractBootstrap.doBind(AbstractBootstrap.java:273)
    at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:269)
    at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:244)
    at org.gawst.proxy.core.ClientListener.run(ClientListener.java:71)
Caused by: java.lang.NoClassDefFoundError: io/netty/buffer/ByteBufAllocator
    at io.netty.channel.DefaultChannelConfig.<init>(DefaultChannelConfig.java:40)
    at io.netty.channel.socket.DefaultServerSocketChannelConfig.<init>(DefaultServerSocketChannelConfig.java:45)
    at io.netty.channel.socket.nio.NioServerSocketChannel$NioServerSocketChannelConfig.<init>(NioServerSocketChannel.java:189)
    at io.netty.channel.socket.nio.NioServerSocketChannel$NioServerSocketChannelConfig.<init>(NioServerSocketChannel.java:187)
    at io.netty.channel.socket.nio.NioServerSocketChannel.<init>(NioServerSocketChannel.java:85)
    at io.netty.channel.socket.nio.NioServerSocketChannel.<init>(NioServerSocketChannel.java:70)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at io.netty.bootstrap.AbstractBootstrap$BootstrapChannelFactory.newChannel(AbstractBootstrap.java:445)
    at io.netty.bootstrap.AbstractBootstrap.initAndRegister(AbstractBootstrap.java:298)
    at io.netty.bootstrap.AbstractBootstrap.doBind(AbstractBootstrap.java:273)
    at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:269)
    at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:244)
Please sign in to comment.