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