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