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