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