View Javadoc
1   /*
2    * Copyright 2016 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    *   https://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 io.netty.channel;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufAllocator;
20  import io.netty.buffer.CompositeByteBuf;
21  import io.netty.util.internal.ObjectUtil;
22  import io.netty.util.internal.UnstableApi;
23  
24  /**
25   * Wraps another {@link ByteBufAllocator} and use heapbuffers everywhere except when a direct buffer is explicit
26   * requested.
27   */
28  @UnstableApi
29  public final class PreferHeapByteBufAllocator implements ByteBufAllocator {
30      private final ByteBufAllocator allocator;
31  
32      public PreferHeapByteBufAllocator(ByteBufAllocator allocator) {
33          this.allocator = ObjectUtil.checkNotNull(allocator, "allocator");
34      }
35  
36      @Override
37      public ByteBuf buffer() {
38          return allocator.heapBuffer();
39      }
40  
41      @Override
42      public ByteBuf buffer(int initialCapacity) {
43          return allocator.heapBuffer(initialCapacity);
44      }
45  
46      @Override
47      public ByteBuf buffer(int initialCapacity, int maxCapacity) {
48          return allocator.heapBuffer(initialCapacity, maxCapacity);
49      }
50  
51      @Override
52      public ByteBuf ioBuffer() {
53          return allocator.heapBuffer();
54      }
55  
56      @Override
57      public ByteBuf ioBuffer(int initialCapacity) {
58          return allocator.heapBuffer(initialCapacity);
59      }
60  
61      @Override
62      public ByteBuf ioBuffer(int initialCapacity, int maxCapacity) {
63          return allocator.heapBuffer(initialCapacity, maxCapacity);
64      }
65  
66      @Override
67      public ByteBuf heapBuffer() {
68          return allocator.heapBuffer();
69      }
70  
71      @Override
72      public ByteBuf heapBuffer(int initialCapacity) {
73          return allocator.heapBuffer(initialCapacity);
74      }
75  
76      @Override
77      public ByteBuf heapBuffer(int initialCapacity, int maxCapacity) {
78          return allocator.heapBuffer(initialCapacity, maxCapacity);
79      }
80  
81      @Override
82      public ByteBuf directBuffer() {
83          return allocator.directBuffer();
84      }
85  
86      @Override
87      public ByteBuf directBuffer(int initialCapacity) {
88          return allocator.directBuffer(initialCapacity);
89      }
90  
91      @Override
92      public ByteBuf directBuffer(int initialCapacity, int maxCapacity) {
93          return allocator.directBuffer(initialCapacity, maxCapacity);
94      }
95  
96      @Override
97      public CompositeByteBuf compositeBuffer() {
98          return allocator.compositeHeapBuffer();
99      }
100 
101     @Override
102     public CompositeByteBuf compositeBuffer(int maxNumComponents) {
103         return allocator.compositeHeapBuffer(maxNumComponents);
104     }
105 
106     @Override
107     public CompositeByteBuf compositeHeapBuffer() {
108         return allocator.compositeHeapBuffer();
109     }
110 
111     @Override
112     public CompositeByteBuf compositeHeapBuffer(int maxNumComponents) {
113         return allocator.compositeHeapBuffer(maxNumComponents);
114     }
115 
116     @Override
117     public CompositeByteBuf compositeDirectBuffer() {
118         return allocator.compositeDirectBuffer();
119     }
120 
121     @Override
122     public CompositeByteBuf compositeDirectBuffer(int maxNumComponents) {
123         return allocator.compositeDirectBuffer(maxNumComponents);
124     }
125 
126     @Override
127     public boolean isDirectBufferPooled() {
128         return allocator.isDirectBufferPooled();
129     }
130 
131     @Override
132     public int calculateNewCapacity(int minNewCapacity, int maxCapacity) {
133         return allocator.calculateNewCapacity(minNewCapacity, maxCapacity);
134     }
135 }