View Javadoc
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  /**
28   * A derived buffer which exposes its parent's sub-region only.  It is
29   * recommended to use {@link ByteBuf#slice()} and
30   * {@link ByteBuf#slice(int, int)} instead of calling the constructor
31   * explicitly.
32   */
33  public class SlicedByteBuf extends AbstractDerivedByteBuf {
34  
35      private final ByteBuf buffer;
36      private final int adjustment;
37      private final int length;
38  
39      public SlicedByteBuf(ByteBuf buffer, int index, int length) {
40          super(length);
41          if (index < 0 || index > buffer.capacity() - length) {
42              throw new IndexOutOfBoundsException(buffer + ".slice(" + index + ", " + length + ')');
43          }
44  
45          if (buffer instanceof SlicedByteBuf) {
46              this.buffer = ((SlicedByteBuf) buffer).buffer;
47              adjustment = ((SlicedByteBuf) buffer).adjustment + index;
48          } else if (buffer instanceof DuplicatedByteBuf) {
49              this.buffer = buffer.unwrap();
50              adjustment = index;
51          } else {
52              this.buffer = buffer;
53              adjustment = index;
54          }
55          this.length = length;
56  
57          writerIndex(length);
58      }
59  
60      @Override
61      public ByteBuf unwrap() {
62          return buffer;
63      }
64  
65      @Override
66      public ByteBufAllocator alloc() {
67          return buffer.alloc();
68      }
69  
70      @Override
71      public ByteOrder order() {
72          return buffer.order();
73      }
74  
75      @Override
76      public boolean isDirect() {
77          return buffer.isDirect();
78      }
79  
80      @Override
81      public int capacity() {
82          return length;
83      }
84  
85      @Override
86      public ByteBuf capacity(int newCapacity) {
87          throw new UnsupportedOperationException("sliced buffer");
88      }
89  
90      @Override
91      public boolean hasArray() {
92          return buffer.hasArray();
93      }
94  
95      @Override
96      public byte[] array() {
97          return buffer.array();
98      }
99  
100     @Override
101     public int arrayOffset() {
102         return buffer.arrayOffset() + adjustment;
103     }
104 
105     @Override
106     public boolean hasMemoryAddress() {
107         return buffer.hasMemoryAddress();
108     }
109 
110     @Override
111     public long memoryAddress() {
112         return buffer.memoryAddress() + adjustment;
113     }
114 
115     @Override
116     protected byte _getByte(int index) {
117         return buffer.getByte(index + adjustment);
118     }
119 
120     @Override
121     protected short _getShort(int index) {
122         return buffer.getShort(index + adjustment);
123     }
124 
125     @Override
126     protected int _getUnsignedMedium(int index) {
127         return buffer.getUnsignedMedium(index + adjustment);
128     }
129 
130     @Override
131     protected int _getInt(int index) {
132         return buffer.getInt(index + adjustment);
133     }
134 
135     @Override
136     protected long _getLong(int index) {
137         return buffer.getLong(index + adjustment);
138     }
139 
140     @Override
141     public ByteBuf duplicate() {
142         ByteBuf duplicate = buffer.slice(adjustment, length);
143         duplicate.setIndex(readerIndex(), writerIndex());
144         return duplicate;
145     }
146 
147     @Override
148     public ByteBuf copy(int index, int length) {
149         checkIndex(index, length);
150         return buffer.copy(index + adjustment, length);
151     }
152 
153     @Override
154     public ByteBuf slice(int index, int length) {
155         checkIndex(index, length);
156         if (length == 0) {
157             return Unpooled.EMPTY_BUFFER;
158         }
159         return buffer.slice(index + adjustment, length);
160     }
161 
162     @Override
163     public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
164         checkIndex(index, length);
165         buffer.getBytes(index + adjustment, dst, dstIndex, length);
166         return this;
167     }
168 
169     @Override
170     public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
171         checkIndex(index, length);
172         buffer.getBytes(index + adjustment, dst, dstIndex, length);
173         return this;
174     }
175 
176     @Override
177     public ByteBuf getBytes(int index, ByteBuffer dst) {
178         checkIndex(index, dst.remaining());
179         buffer.getBytes(index + adjustment, dst);
180         return this;
181     }
182 
183     @Override
184     protected void _setByte(int index, int value) {
185         buffer.setByte(index + adjustment, value);
186     }
187 
188     @Override
189     protected void _setShort(int index, int value) {
190         buffer.setShort(index + adjustment, value);
191     }
192 
193     @Override
194     protected void _setMedium(int index, int value) {
195         buffer.setMedium(index + adjustment, value);
196     }
197 
198     @Override
199     protected void _setInt(int index, int value) {
200         buffer.setInt(index + adjustment, value);
201     }
202 
203     @Override
204     protected void _setLong(int index, long value) {
205         buffer.setLong(index + adjustment, value);
206     }
207 
208     @Override
209     public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
210         checkIndex(index, length);
211         buffer.setBytes(index + adjustment, src, srcIndex, length);
212         return this;
213     }
214 
215     @Override
216     public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
217         checkIndex(index, length);
218         buffer.setBytes(index + adjustment, src, srcIndex, length);
219         return this;
220     }
221 
222     @Override
223     public ByteBuf setBytes(int index, ByteBuffer src) {
224         checkIndex(index, src.remaining());
225         buffer.setBytes(index + adjustment, src);
226         return this;
227     }
228 
229     @Override
230     public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
231         checkIndex(index, length);
232         buffer.getBytes(index + adjustment, out, length);
233         return this;
234     }
235 
236     @Override
237     public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
238         checkIndex(index, length);
239         return buffer.getBytes(index + adjustment, out, length);
240     }
241 
242     @Override
243     public int setBytes(int index, InputStream in, int length) throws IOException {
244         checkIndex(index, length);
245         return buffer.setBytes(index + adjustment, in, length);
246     }
247 
248     @Override
249     public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
250         checkIndex(index, length);
251         return buffer.setBytes(index + adjustment, in, length);
252     }
253 
254     @Override
255     public int nioBufferCount() {
256         return buffer.nioBufferCount();
257     }
258 
259     @Override
260     public ByteBuffer nioBuffer(int index, int length) {
261         checkIndex(index, length);
262         return buffer.nioBuffer(index + adjustment, length);
263     }
264 
265     @Override
266     public ByteBuffer[] nioBuffers(int index, int length) {
267         checkIndex(index, length);
268         return buffer.nioBuffers(index + adjustment, length);
269     }
270 
271     @Override
272     public ByteBuffer internalNioBuffer(int index, int length) {
273         checkIndex(index, length);
274         return nioBuffer(index, length);
275     }
276 
277     @Override
278     public int forEachByte(int index, int length, ByteBufProcessor processor) {
279         int ret = buffer.forEachByte(index + adjustment, length, processor);
280         if (ret >= adjustment) {
281             return ret - adjustment;
282         } else {
283             return -1;
284         }
285     }
286 
287     @Override
288     public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
289         int ret = buffer.forEachByteDesc(index + adjustment, length, processor);
290         if (ret >= adjustment) {
291             return ret - adjustment;
292         } else {
293             return -1;
294         }
295     }
296 }