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 org.jboss.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 ChannelBuffer#duplicate()} instead
30   * of calling the constructor explicitly.
31   */
32  public class DuplicatedChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
33  
34      private final ChannelBuffer buffer;
35  
36      public DuplicatedChannelBuffer(ChannelBuffer buffer) {
37          if (buffer == null) {
38              throw new NullPointerException("buffer");
39          }
40          this.buffer = buffer;
41          setIndex(buffer.readerIndex(), buffer.writerIndex());
42      }
43  
44      private DuplicatedChannelBuffer(DuplicatedChannelBuffer buffer) {
45          this.buffer = buffer.buffer;
46          setIndex(buffer.readerIndex(), buffer.writerIndex());
47      }
48  
49      public ChannelBuffer unwrap() {
50          return buffer;
51      }
52  
53      public ChannelBufferFactory factory() {
54          return buffer.factory();
55      }
56  
57      public ByteOrder order() {
58          return buffer.order();
59      }
60  
61      public boolean isDirect() {
62          return buffer.isDirect();
63      }
64  
65      public int capacity() {
66          return buffer.capacity();
67      }
68  
69      public boolean hasArray() {
70          return buffer.hasArray();
71      }
72  
73      public byte[] array() {
74          return buffer.array();
75      }
76  
77      public int arrayOffset() {
78          return buffer.arrayOffset();
79      }
80  
81      public byte getByte(int index) {
82          return buffer.getByte(index);
83      }
84  
85      public short getShort(int index) {
86          return buffer.getShort(index);
87      }
88  
89      public int getUnsignedMedium(int index) {
90          return buffer.getUnsignedMedium(index);
91      }
92  
93      public int getInt(int index) {
94          return buffer.getInt(index);
95      }
96  
97      public long getLong(int index) {
98          return buffer.getLong(index);
99      }
100 
101     public ChannelBuffer duplicate() {
102         return new DuplicatedChannelBuffer(this);
103     }
104 
105     public ChannelBuffer copy(int index, int length) {
106         return buffer.copy(index, length);
107     }
108 
109     public ChannelBuffer slice(int index, int length) {
110         return buffer.slice(index, length);
111     }
112 
113     public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
114         buffer.getBytes(index, dst, dstIndex, length);
115     }
116 
117     public void getBytes(int index, byte[] dst, int dstIndex, int length) {
118         buffer.getBytes(index, dst, dstIndex, length);
119     }
120 
121     public void getBytes(int index, ByteBuffer dst) {
122         buffer.getBytes(index, dst);
123     }
124 
125     public void setByte(int index, int value) {
126         buffer.setByte(index, value);
127     }
128 
129     public void setShort(int index, int value) {
130         buffer.setShort(index, value);
131     }
132 
133     public void setMedium(int index, int value) {
134         buffer.setMedium(index, value);
135     }
136 
137     public void setInt(int index, int value) {
138         buffer.setInt(index, value);
139     }
140 
141     public void setLong(int index, long value) {
142         buffer.setLong(index, value);
143     }
144 
145     public void setBytes(int index, byte[] src, int srcIndex, int length) {
146         buffer.setBytes(index, src, srcIndex, length);
147     }
148 
149     public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
150         buffer.setBytes(index, src, srcIndex, length);
151     }
152 
153     public void setBytes(int index, ByteBuffer src) {
154         buffer.setBytes(index, src);
155     }
156 
157     public void getBytes(int index, OutputStream out, int length)
158             throws IOException {
159         buffer.getBytes(index, out, length);
160     }
161 
162     public int getBytes(int index, GatheringByteChannel out, int length)
163             throws IOException {
164         return buffer.getBytes(index, out, length);
165     }
166 
167     public int setBytes(int index, InputStream in, int length)
168             throws IOException {
169         return buffer.setBytes(index, in, length);
170     }
171 
172     public int setBytes(int index, ScatteringByteChannel in, int length)
173             throws IOException {
174         return buffer.setBytes(index, in, length);
175     }
176 
177     public ByteBuffer toByteBuffer(int index, int length) {
178         return buffer.toByteBuffer(index, length);
179     }
180 }