View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.channel.socket.nio;
17  
18  import org.jboss.netty.util.ExternalResourceReleasable;
19  import org.jboss.netty.util.internal.ByteBufferUtil;
20  
21  import java.nio.ByteBuffer;
22  
23  final class SocketReceiveBufferAllocator implements ExternalResourceReleasable {
24  
25      private ByteBuffer buf;
26      private int exceedCount;
27      private final int maxExceedCount;
28      private final int percentual;
29  
30      SocketReceiveBufferAllocator() {
31          this(16, 80);
32      }
33  
34      SocketReceiveBufferAllocator(int maxExceedCount, int percentual) {
35          this.maxExceedCount = maxExceedCount;
36          this.percentual = percentual;
37      }
38  
39  
40      ByteBuffer get(int size) {
41          if (buf == null) {
42              return newBuffer(size);
43          } else if (buf.capacity() < size) {
44              return newBuffer(size);
45          } else if (buf.capacity() * percentual / 100 > size) {
46              if (++exceedCount == maxExceedCount) {
47                 return newBuffer(size);
48              } else {
49                  buf.clear();
50              }
51          } else {
52              exceedCount = 0;
53              buf.clear();
54          }
55          return buf;
56      }
57  
58      private ByteBuffer newBuffer(int size) {
59          if (buf != null) {
60              exceedCount = 0;
61              ByteBufferUtil.destroy(buf);
62          }
63          buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
64          return buf;
65      }
66  
67      private static int normalizeCapacity(int capacity) {
68          // Normalize to multiple of 1024
69          int q = capacity >>> 10;
70          int r = capacity & 1023;
71          if (r != 0) {
72              q ++;
73          }
74          return q << 10;
75      }
76  
77      public void releaseExternalResources() {
78          if (buf != null) {
79              ByteBufferUtil.destroy(buf);
80          }
81      }
82  }