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