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