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