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    *   https://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 io.netty.util.ByteProcessor;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.nio.ByteBuffer;
24  import java.nio.ByteOrder;
25  import java.nio.ReadOnlyBufferException;
26  import java.nio.channels.FileChannel;
27  import java.nio.channels.GatheringByteChannel;
28  import java.nio.channels.ScatteringByteChannel;
29  
30  /**
31   * A derived buffer which forbids any write requests to its parent.  It is
32   * recommended to use {@link Unpooled#unmodifiableBuffer(ByteBuf)}
33   * instead of calling the constructor explicitly.
34   *
35   * @deprecated Do not use.
36   */
37  @Deprecated
38  public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
39  
40      private final ByteBuf buffer;
41  
42      public ReadOnlyByteBuf(ByteBuf buffer) {
43          super(buffer.maxCapacity());
44  
45          if (buffer instanceof ReadOnlyByteBuf || buffer instanceof DuplicatedByteBuf) {
46              this.buffer = buffer.unwrap();
47          } else {
48              this.buffer = buffer;
49          }
50          setIndex(buffer.readerIndex(), buffer.writerIndex());
51      }
52  
53      @Override
54      public boolean isReadOnly() {
55          return true;
56      }
57  
58      @Override
59      public boolean isWritable() {
60          return false;
61      }
62  
63      @Override
64      public boolean isWritable(int numBytes) {
65          return false;
66      }
67  
68      @Override
69      public int ensureWritable(int minWritableBytes, boolean force) {
70          return 1;
71      }
72  
73      @Override
74      public ByteBuf ensureWritable(int minWritableBytes) {
75          throw new ReadOnlyBufferException();
76      }
77  
78      @Override
79      public ByteBuf unwrap() {
80          return buffer;
81      }
82  
83      @Override
84      public ByteBufAllocator alloc() {
85          return unwrap().alloc();
86      }
87  
88      @Override
89      @Deprecated
90      public ByteOrder order() {
91          return unwrap().order();
92      }
93  
94      @Override
95      public boolean isDirect() {
96          return unwrap().isDirect();
97      }
98  
99      @Override
100     public boolean hasArray() {
101         return false;
102     }
103 
104     @Override
105     public byte[] array() {
106         throw new ReadOnlyBufferException();
107     }
108 
109     @Override
110     public int arrayOffset() {
111         throw new ReadOnlyBufferException();
112     }
113 
114     @Override
115     public boolean hasMemoryAddress() {
116         return unwrap().hasMemoryAddress();
117     }
118 
119     @Override
120     public long memoryAddress() {
121         return unwrap().memoryAddress();
122     }
123 
124     @Override
125     public ByteBuf discardReadBytes() {
126         throw new ReadOnlyBufferException();
127     }
128 
129     @Override
130     public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
131         throw new ReadOnlyBufferException();
132     }
133 
134     @Override
135     public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
136         throw new ReadOnlyBufferException();
137     }
138 
139     @Override
140     public ByteBuf setBytes(int index, ByteBuffer src) {
141         throw new ReadOnlyBufferException();
142     }
143 
144     @Override
145     public ByteBuf setByte(int index, int value) {
146         throw new ReadOnlyBufferException();
147     }
148 
149     @Override
150     protected void _setByte(int index, int value) {
151         throw new ReadOnlyBufferException();
152     }
153 
154     @Override
155     public ByteBuf setShort(int index, int value) {
156         throw new ReadOnlyBufferException();
157     }
158 
159     @Override
160     protected void _setShort(int index, int value) {
161         throw new ReadOnlyBufferException();
162     }
163 
164     @Override
165     public ByteBuf setShortLE(int index, int value) {
166         throw new ReadOnlyBufferException();
167     }
168 
169     @Override
170     protected void _setShortLE(int index, int value) {
171         throw new ReadOnlyBufferException();
172     }
173 
174     @Override
175     public ByteBuf setMedium(int index, int value) {
176         throw new ReadOnlyBufferException();
177     }
178 
179     @Override
180     protected void _setMedium(int index, int value) {
181         throw new ReadOnlyBufferException();
182     }
183 
184     @Override
185     public ByteBuf setMediumLE(int index, int value) {
186         throw new ReadOnlyBufferException();
187     }
188 
189     @Override
190     protected void _setMediumLE(int index, int value) {
191         throw new ReadOnlyBufferException();
192     }
193 
194     @Override
195     public ByteBuf setInt(int index, int value) {
196         throw new ReadOnlyBufferException();
197     }
198 
199     @Override
200     protected void _setInt(int index, int value) {
201         throw new ReadOnlyBufferException();
202     }
203 
204     @Override
205     public ByteBuf setIntLE(int index, int value) {
206         throw new ReadOnlyBufferException();
207     }
208 
209     @Override
210     protected void _setIntLE(int index, int value) {
211         throw new ReadOnlyBufferException();
212     }
213 
214     @Override
215     public ByteBuf setLong(int index, long value) {
216         throw new ReadOnlyBufferException();
217     }
218 
219     @Override
220     protected void _setLong(int index, long value) {
221         throw new ReadOnlyBufferException();
222     }
223 
224     @Override
225     public ByteBuf setLongLE(int index, long value) {
226         throw new ReadOnlyBufferException();
227     }
228 
229     @Override
230     protected void _setLongLE(int index, long value) {
231         throw new ReadOnlyBufferException();
232     }
233 
234     @Override
235     public int setBytes(int index, InputStream in, int length) {
236         throw new ReadOnlyBufferException();
237     }
238 
239     @Override
240     public int setBytes(int index, ScatteringByteChannel in, int length) {
241         throw new ReadOnlyBufferException();
242     }
243 
244     @Override
245     public int setBytes(int index, FileChannel in, long position, int length) {
246         throw new ReadOnlyBufferException();
247     }
248 
249     @Override
250     public int getBytes(int index, GatheringByteChannel out, int length)
251             throws IOException {
252         return unwrap().getBytes(index, out, length);
253     }
254 
255     @Override
256     public int getBytes(int index, FileChannel out, long position, int length)
257             throws IOException {
258         return unwrap().getBytes(index, out, position, length);
259     }
260 
261     @Override
262     public ByteBuf getBytes(int index, OutputStream out, int length)
263             throws IOException {
264         unwrap().getBytes(index, out, length);
265         return this;
266     }
267 
268     @Override
269     public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
270         unwrap().getBytes(index, dst, dstIndex, length);
271         return this;
272     }
273 
274     @Override
275     public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
276         unwrap().getBytes(index, dst, dstIndex, length);
277         return this;
278     }
279 
280     @Override
281     public ByteBuf getBytes(int index, ByteBuffer dst) {
282         unwrap().getBytes(index, dst);
283         return this;
284     }
285 
286     @Override
287     public ByteBuf duplicate() {
288         return new ReadOnlyByteBuf(this);
289     }
290 
291     @Override
292     public ByteBuf copy(int index, int length) {
293         return unwrap().copy(index, length);
294     }
295 
296     @Override
297     public ByteBuf slice(int index, int length) {
298         return new ReadOnlyByteBuf(unwrap().slice(index, length));
299     }
300 
301     @Override
302     public byte getByte(int index) {
303         return unwrap().getByte(index);
304     }
305 
306     @Override
307     protected byte _getByte(int index) {
308         return unwrap().getByte(index);
309     }
310 
311     @Override
312     public short getShort(int index) {
313         return unwrap().getShort(index);
314     }
315 
316     @Override
317     protected short _getShort(int index) {
318         return unwrap().getShort(index);
319     }
320 
321     @Override
322     public short getShortLE(int index) {
323         return unwrap().getShortLE(index);
324     }
325 
326     @Override
327     protected short _getShortLE(int index) {
328         return unwrap().getShortLE(index);
329     }
330 
331     @Override
332     public int getUnsignedMedium(int index) {
333         return unwrap().getUnsignedMedium(index);
334     }
335 
336     @Override
337     protected int _getUnsignedMedium(int index) {
338         return unwrap().getUnsignedMedium(index);
339     }
340 
341     @Override
342     public int getUnsignedMediumLE(int index) {
343         return unwrap().getUnsignedMediumLE(index);
344     }
345 
346     @Override
347     protected int _getUnsignedMediumLE(int index) {
348         return unwrap().getUnsignedMediumLE(index);
349     }
350 
351     @Override
352     public int getInt(int index) {
353         return unwrap().getInt(index);
354     }
355 
356     @Override
357     protected int _getInt(int index) {
358         return unwrap().getInt(index);
359     }
360 
361     @Override
362     public int getIntLE(int index) {
363         return unwrap().getIntLE(index);
364     }
365 
366     @Override
367     protected int _getIntLE(int index) {
368         return unwrap().getIntLE(index);
369     }
370 
371     @Override
372     public long getLong(int index) {
373         return unwrap().getLong(index);
374     }
375 
376     @Override
377     protected long _getLong(int index) {
378         return unwrap().getLong(index);
379     }
380 
381     @Override
382     public long getLongLE(int index) {
383         return unwrap().getLongLE(index);
384     }
385 
386     @Override
387     protected long _getLongLE(int index) {
388         return unwrap().getLongLE(index);
389     }
390 
391     @Override
392     public int nioBufferCount() {
393         return unwrap().nioBufferCount();
394     }
395 
396     @Override
397     public ByteBuffer nioBuffer(int index, int length) {
398         return unwrap().nioBuffer(index, length).asReadOnlyBuffer();
399     }
400 
401     @Override
402     public ByteBuffer[] nioBuffers(int index, int length) {
403         ByteBuffer[] buffers = unwrap().nioBuffers(index, length);
404         for (int i = 0; i < buffers.length; i++) {
405             ByteBuffer buf = buffers[i];
406             if (!buf.isReadOnly()) {
407                 buffers[i] = buf.asReadOnlyBuffer();
408             }
409         }
410         return buffers;
411     }
412 
413     @Override
414     public int forEachByte(int index, int length, ByteProcessor processor) {
415         return unwrap().forEachByte(index, length, processor);
416     }
417 
418     @Override
419     public int forEachByteDesc(int index, int length, ByteProcessor processor) {
420         return unwrap().forEachByteDesc(index, length, processor);
421     }
422 
423     @Override
424     public int capacity() {
425         return unwrap().capacity();
426     }
427 
428     @Override
429     public ByteBuf capacity(int newCapacity) {
430         throw new ReadOnlyBufferException();
431     }
432 
433     @Override
434     public ByteBuf asReadOnly() {
435         return this;
436     }
437 }