查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 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  package io.netty.buffer;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.nio.ByteBuffer;
22  import java.nio.ByteOrder;
23  import java.nio.channels.GatheringByteChannel;
24  import java.nio.channels.ScatteringByteChannel;
25  
26  /**
27   * A derived buffer which exposes its parent's sub-region only.  It is
28   * recommended to use {@link ByteBuf#slice()} and
29   * {@link ByteBuf#slice(int, int)} instead of calling the constructor
30   * explicitly.
31   *
32   * @deprecated Do not use.
33   */
34  @Deprecated
35  public class SlicedByteBuf extends AbstractDerivedByteBuf {
36  
37      private final ByteBuf buffer;
38      private final int adjustment;
39      private final int length;
40  
41      public SlicedByteBuf(ByteBuf buffer, int index, int length) {
42          super(length);
43          if (index < 0 || index > buffer.capacity() - length) {
44              throw new IndexOutOfBoundsException(buffer + ".slice(" + index + ", " + length + ')');
45          }
46  
47          if (buffer instanceof SlicedByteBuf) {
48              this.buffer = ((SlicedByteBuf) buffer).buffer;
49              adjustment = ((SlicedByteBuf) buffer).adjustment + index;
50          } else if (buffer instanceof DuplicatedByteBuf) {
51              this.buffer = buffer.unwrap();
52              adjustment = index;
53          } else {
54              this.buffer = buffer;
55              adjustment = index;
56          }
57          this.length = length;
58  
59          writerIndex(length);
60      }
61  
62      @Override
63      public ByteBuf unwrap() {
64          return buffer;
65      }
66  
67      @Override
68      public ByteBufAllocator alloc() {
69          return unwrap().alloc();
70      }
71  
72      @Override
73      @Deprecated
74      public ByteOrder order() {
75          return unwrap().order();
76      }
77  
78      @Override
79      public boolean isDirect() {
80          return unwrap().isDirect();
81      }
82  
83      @Override
84      public int capacity() {
85          return length;
86      }
87  
88      @Override
89      public ByteBuf capacity(int newCapacity) {
90          throw new UnsupportedOperationException("sliced buffer");
91      }
92  
93      @Override
94      public boolean hasArray() {
95          return unwrap().hasArray();
96      }
97  
98      @Override
99      public byte[] array() {
100         return unwrap().array();
101     }
102 
103     @Override
104     public int arrayOffset() {
105         return idx(unwrap().arrayOffset());
106     }
107 
108     @Override
109     public boolean hasMemoryAddress() {
110         return unwrap().hasMemoryAddress();
111     }
112 
113     @Override
114     public long memoryAddress() {
115         return unwrap().memoryAddress() + adjustment;
116     }
117 
118     @Override
119     public byte getByte(int index) {
120         checkIndex0(index, 1);
121         return unwrap().getByte(idx(index));
122     }
123 
124     @Override
125     protected byte _getByte(int index) {
126         return unwrap().getByte(idx(index));
127     }
128 
129     @Override
130     public short getShort(int index) {
131         checkIndex0(index, 2);
132         return unwrap().getShort(idx(index));
133     }
134 
135     @Override
136     protected short _getShort(int index) {
137         return unwrap().getShort(idx(index));
138     }
139 
140     @Override
141     public int getUnsignedMedium(int index) {
142         checkIndex0(index, 3);
143         return unwrap().getUnsignedMedium(idx(index));
144     }
145 
146     @Override
147     protected int _getUnsignedMedium(int index) {
148         return unwrap().getUnsignedMedium(idx(index));
149     }
150 
151     @Override
152     public int getInt(int index) {
153         checkIndex0(index, 4);
154         return unwrap().getInt(idx(index));
155     }
156 
157     @Override
158     protected int _getInt(int index) {
159         return unwrap().getInt(idx(index));
160     }
161 
162     @Override
163     public long getLong(int index) {
164         checkIndex0(index, 8);
165         return unwrap().getLong(idx(index));
166     }
167 
168     @Override
169     protected long _getLong(int index) {
170         return unwrap().getLong(idx(index));
171     }
172 
173     @Override
174     public ByteBuf duplicate() {
175         return unwrap().duplicate().setIndex(idx(readerIndex()), idx(writerIndex()));
176     }
177 
178     @Override
179     public ByteBuf copy(int index, int length) {
180         checkIndex0(index, length);
181         return unwrap().copy(idx(index), length);
182     }
183 
184     @Override
185     public ByteBuf slice(int index, int length) {
186         checkIndex0(index, length);
187         return unwrap().slice(idx(index), length);
188     }
189 
190     @Override
191     public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
192         checkIndex0(index, length);
193         unwrap().getBytes(idx(index), dst, dstIndex, length);
194         return this;
195     }
196 
197     @Override
198     public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
199         checkIndex0(index, length);
200         unwrap().getBytes(idx(index), dst, dstIndex, length);
201         return this;
202     }
203 
204     @Override
205     public ByteBuf getBytes(int index, ByteBuffer dst) {
206         checkIndex0(index, dst.remaining());
207         unwrap().getBytes(idx(index), dst);
208         return this;
209     }
210 
211     @Override
212     public ByteBuf setByte(int index, int value) {
213         checkIndex0(index, 1);
214         unwrap().setByte(idx(index), value);
215         return this;
216     }
217 
218     @Override
219     protected void _setByte(int index, int value) {
220         unwrap().setByte(idx(index), value);
221     }
222 
223     @Override
224     public ByteBuf setShort(int index, int value) {
225         checkIndex0(index, 2);
226         unwrap().setShort(idx(index), value);
227         return this;
228     }
229 
230     @Override
231     protected void _setShort(int index, int value) {
232         unwrap().setShort(idx(index), value);
233     }
234 
235     @Override
236     public ByteBuf setMedium(int index, int value) {
237         checkIndex0(index, 3);
238         unwrap().setMedium(idx(index), value);
239         return this;
240     }
241 
242     @Override
243     protected void _setMedium(int index, int value) {
244         unwrap().setMedium(idx(index), value);
245     }
246 
247     @Override
248     public ByteBuf setInt(int index, int value) {
249         checkIndex0(index, 4);
250         unwrap().setInt(idx(index), value);
251         return this;
252     }
253 
254     @Override
255     protected void _setInt(int index, int value) {
256         unwrap().setInt(idx(index), value);
257     }
258 
259     @Override
260     public ByteBuf setLong(int index, long value) {
261         checkIndex0(index, 8);
262         unwrap().setLong(idx(index), value);
263         return this;
264     }
265 
266     @Override
267     protected void _setLong(int index, long value) {
268         unwrap().setLong(idx(index), value);
269     }
270 
271     @Override
272     public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
273         checkIndex0(index, length);
274         unwrap().setBytes(idx(index), src, srcIndex, length);
275         return this;
276     }
277 
278     @Override
279     public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
280         checkIndex0(index, length);
281         unwrap().setBytes(idx(index), src, srcIndex, length);
282         return this;
283     }
284 
285     @Override
286     public ByteBuf setBytes(int index, ByteBuffer src) {
287         checkIndex0(index, src.remaining());
288         unwrap().setBytes(idx(index), src);
289         return this;
290     }
291 
292     @Override
293     public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
294         checkIndex0(index, length);
295         unwrap().getBytes(idx(index), out, length);
296         return this;
297     }
298 
299     @Override
300     public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
301         checkIndex0(index, length);
302         return unwrap().getBytes(idx(index), out, length);
303     }
304 
305     @Override
306     public int setBytes(int index, InputStream in, int length) throws IOException {
307         checkIndex0(index, length);
308         return unwrap().setBytes(idx(index), in, length);
309     }
310 
311     @Override
312     public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
313         checkIndex0(index, length);
314         return unwrap().setBytes(idx(index), in, length);
315     }
316 
317     @Override
318     public int nioBufferCount() {
319         return unwrap().nioBufferCount();
320     }
321 
322     @Override
323     public ByteBuffer nioBuffer(int index, int length) {
324         checkIndex0(index, length);
325         return unwrap().nioBuffer(idx(index), length);
326     }
327 
328     @Override
329     public ByteBuffer[] nioBuffers(int index, int length) {
330         checkIndex0(index, length);
331         return unwrap().nioBuffers(idx(index), length);
332     }
333 
334     @Override
335     public int forEachByte(int index, int length, ByteBufProcessor processor) {
336         checkIndex0(index, length);
337         int ret = unwrap().forEachByte(idx(index), length, processor);
338         if (ret >= adjustment) {
339             return ret - adjustment;
340         } else {
341             return -1;
342         }
343     }
344 
345     @Override
346     public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
347         checkIndex0(index, length);
348         int ret = unwrap().forEachByteDesc(idx(index), length, processor);
349         if (ret >= adjustment) {
350             return ret - adjustment;
351         } else {
352             return -1;
353         }
354     }
355 
356     /**
357      * Returns the index with the needed adjustment.
358      */
359     final int idx(int index) {
360         return index + adjustment;
361     }
362 }