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  
17  package io.netty.buffer;
18  
19  import io.netty.util.ByteProcessor;
20  import io.netty.util.internal.ObjectPool;
21  import io.netty.util.internal.ObjectPool.Handle;
22  import io.netty.util.internal.ObjectPool.ObjectCreator;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.nio.ByteBuffer;
28  import java.nio.channels.FileChannel;
29  import java.nio.channels.GatheringByteChannel;
30  import java.nio.channels.ScatteringByteChannel;
31  
32  final class PooledDuplicatedByteBuf extends AbstractPooledDerivedByteBuf {
33  
34      private static final ObjectPool<PooledDuplicatedByteBuf> RECYCLER = ObjectPool.newPool(
35              new ObjectCreator<PooledDuplicatedByteBuf>() {
36          @Override
37          public PooledDuplicatedByteBuf newObject(Handle<PooledDuplicatedByteBuf> handle) {
38              return new PooledDuplicatedByteBuf(handle);
39          }
40      });
41  
42      static PooledDuplicatedByteBuf newInstance(AbstractByteBuf unwrapped, ByteBuf wrapped,
43                                                 int readerIndex, int writerIndex) {
44          final PooledDuplicatedByteBuf duplicate = RECYCLER.get();
45          duplicate.init(unwrapped, wrapped, readerIndex, writerIndex, unwrapped.maxCapacity());
46          duplicate.markReaderIndex();
47          duplicate.markWriterIndex();
48  
49          return duplicate;
50      }
51  
52      private PooledDuplicatedByteBuf(Handle<PooledDuplicatedByteBuf> handle) {
53          super(handle);
54      }
55  
56      @Override
57      public int capacity() {
58          return unwrap().capacity();
59      }
60  
61      @Override
62      public ByteBuf capacity(int newCapacity) {
63          unwrap().capacity(newCapacity);
64          return this;
65      }
66  
67      @Override
68      public int arrayOffset() {
69          return unwrap().arrayOffset();
70      }
71  
72      @Override
73      public long memoryAddress() {
74          return unwrap().memoryAddress();
75      }
76  
77      @Override
78      public ByteBuffer nioBuffer(int index, int length) {
79          return unwrap().nioBuffer(index, length);
80      }
81  
82      @Override
83      public ByteBuffer[] nioBuffers(int index, int length) {
84          return unwrap().nioBuffers(index, length);
85      }
86  
87      @Override
88      public ByteBuf copy(int index, int length) {
89          return unwrap().copy(index, length);
90      }
91  
92      @Override
93      public ByteBuf retainedSlice(int index, int length) {
94          return PooledSlicedByteBuf.newInstance(unwrap(), this, index, length);
95      }
96  
97      @Override
98      public ByteBuf duplicate() {
99          return duplicate0().setIndex(readerIndex(), writerIndex());
100     }
101 
102     @Override
103     public ByteBuf retainedDuplicate() {
104         return PooledDuplicatedByteBuf.newInstance(unwrap(), this, readerIndex(), writerIndex());
105     }
106 
107     @Override
108     public byte getByte(int index) {
109         return unwrap().getByte(index);
110     }
111 
112     @Override
113     protected byte _getByte(int index) {
114         return unwrap()._getByte(index);
115     }
116 
117     @Override
118     public short getShort(int index) {
119         return unwrap().getShort(index);
120     }
121 
122     @Override
123     protected short _getShort(int index) {
124         return unwrap()._getShort(index);
125     }
126 
127     @Override
128     public short getShortLE(int index) {
129         return unwrap().getShortLE(index);
130     }
131 
132     @Override
133     protected short _getShortLE(int index) {
134         return unwrap()._getShortLE(index);
135     }
136 
137     @Override
138     public int getUnsignedMedium(int index) {
139         return unwrap().getUnsignedMedium(index);
140     }
141 
142     @Override
143     protected int _getUnsignedMedium(int index) {
144         return unwrap()._getUnsignedMedium(index);
145     }
146 
147     @Override
148     public int getUnsignedMediumLE(int index) {
149         return unwrap().getUnsignedMediumLE(index);
150     }
151 
152     @Override
153     protected int _getUnsignedMediumLE(int index) {
154         return unwrap()._getUnsignedMediumLE(index);
155     }
156 
157     @Override
158     public int getInt(int index) {
159         return unwrap().getInt(index);
160     }
161 
162     @Override
163     protected int _getInt(int index) {
164         return unwrap()._getInt(index);
165     }
166 
167     @Override
168     public int getIntLE(int index) {
169         return unwrap().getIntLE(index);
170     }
171 
172     @Override
173     protected int _getIntLE(int index) {
174         return unwrap()._getIntLE(index);
175     }
176 
177     @Override
178     public long getLong(int index) {
179         return unwrap().getLong(index);
180     }
181 
182     @Override
183     protected long _getLong(int index) {
184         return unwrap()._getLong(index);
185     }
186 
187     @Override
188     public long getLongLE(int index) {
189         return unwrap().getLongLE(index);
190     }
191 
192     @Override
193     protected long _getLongLE(int index) {
194         return unwrap()._getLongLE(index);
195     }
196 
197     @Override
198     public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
199         unwrap().getBytes(index, dst, dstIndex, length);
200         return this;
201     }
202 
203     @Override
204     public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
205         unwrap().getBytes(index, dst, dstIndex, length);
206         return this;
207     }
208 
209     @Override
210     public ByteBuf getBytes(int index, ByteBuffer dst) {
211         unwrap().getBytes(index, dst);
212         return this;
213     }
214 
215     @Override
216     public ByteBuf setByte(int index, int value) {
217         unwrap().setByte(index, value);
218         return this;
219     }
220 
221     @Override
222     protected void _setByte(int index, int value) {
223         unwrap()._setByte(index, value);
224     }
225 
226     @Override
227     public ByteBuf setShort(int index, int value) {
228         unwrap().setShort(index, value);
229         return this;
230     }
231 
232     @Override
233     protected void _setShort(int index, int value) {
234         unwrap()._setShort(index, value);
235     }
236 
237     @Override
238     public ByteBuf setShortLE(int index, int value) {
239         unwrap().setShortLE(index, value);
240         return this;
241     }
242 
243     @Override
244     protected void _setShortLE(int index, int value) {
245         unwrap()._setShortLE(index, value);
246     }
247 
248     @Override
249     public ByteBuf setMedium(int index, int value) {
250         unwrap().setMedium(index, value);
251         return this;
252     }
253 
254     @Override
255     protected void _setMedium(int index, int value) {
256         unwrap()._setMedium(index, value);
257     }
258 
259     @Override
260     public ByteBuf setMediumLE(int index, int value) {
261         unwrap().setMediumLE(index, value);
262         return this;
263     }
264 
265     @Override
266     protected void _setMediumLE(int index, int value) {
267         unwrap()._setMediumLE(index, value);
268     }
269 
270     @Override
271     public ByteBuf setInt(int index, int value) {
272         unwrap().setInt(index, value);
273         return this;
274     }
275 
276     @Override
277     protected void _setInt(int index, int value) {
278         unwrap()._setInt(index, value);
279     }
280 
281     @Override
282     public ByteBuf setIntLE(int index, int value) {
283         unwrap().setIntLE(index, value);
284         return this;
285     }
286 
287     @Override
288     protected void _setIntLE(int index, int value) {
289         unwrap()._setIntLE(index, value);
290     }
291 
292     @Override
293     public ByteBuf setLong(int index, long value) {
294         unwrap().setLong(index, value);
295         return this;
296     }
297 
298     @Override
299     protected void _setLong(int index, long value) {
300         unwrap()._setLong(index, value);
301     }
302 
303     @Override
304     public ByteBuf setLongLE(int index, long value) {
305         unwrap().setLongLE(index, value);
306         return this;
307     }
308 
309     @Override
310     protected void _setLongLE(int index, long value) {
311         unwrap().setLongLE(index, value);
312     }
313 
314     @Override
315     public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
316         unwrap().setBytes(index, src, srcIndex, length);
317         return this;
318     }
319 
320     @Override
321     public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
322         unwrap().setBytes(index, src, srcIndex, length);
323         return this;
324     }
325 
326     @Override
327     public ByteBuf setBytes(int index, ByteBuffer src) {
328         unwrap().setBytes(index, src);
329         return this;
330     }
331 
332     @Override
333     public ByteBuf getBytes(int index, OutputStream out, int length)
334             throws IOException {
335         unwrap().getBytes(index, out, length);
336         return this;
337     }
338 
339     @Override
340     public int getBytes(int index, GatheringByteChannel out, int length)
341             throws IOException {
342         return unwrap().getBytes(index, out, length);
343     }
344 
345     @Override
346     public int getBytes(int index, FileChannel out, long position, int length)
347             throws IOException {
348         return unwrap().getBytes(index, out, position, length);
349     }
350 
351     @Override
352     public int setBytes(int index, InputStream in, int length)
353             throws IOException {
354         return unwrap().setBytes(index, in, length);
355     }
356 
357     @Override
358     public int setBytes(int index, ScatteringByteChannel in, int length)
359             throws IOException {
360         return unwrap().setBytes(index, in, length);
361     }
362 
363     @Override
364     public int setBytes(int index, FileChannel in, long position, int length)
365             throws IOException {
366         return unwrap().setBytes(index, in, position, length);
367     }
368 
369     @Override
370     public int forEachByte(int index, int length, ByteProcessor processor) {
371         return unwrap().forEachByte(index, length, processor);
372     }
373 
374     @Override
375     public int forEachByteDesc(int index, int length, ByteProcessor processor) {
376         return unwrap().forEachByteDesc(index, length, processor);
377     }
378 }