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.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 ChannelBuffers#unmodifiableBuffer(ChannelBuffer)}
30   * instead of calling the constructor explicitly.
31   */
32  public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
33  
34      private final ChannelBuffer buffer;
35  
36      public ReadOnlyChannelBuffer(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 ReadOnlyChannelBuffer(ReadOnlyChannelBuffer 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 boolean hasArray() {
66          return false;
67      }
68  
69      public byte[] array() {
70          throw new ReadOnlyBufferException();
71      }
72  
73      public int arrayOffset() {
74          throw new ReadOnlyBufferException();
75      }
76  
77      @Override
78      public void discardReadBytes() {
79          throw new ReadOnlyBufferException();
80      }
81  
82      public void setByte(int index, int value) {
83          throw new ReadOnlyBufferException();
84      }
85  
86      public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
87          throw new ReadOnlyBufferException();
88      }
89  
90      public void setBytes(int index, byte[] src, int srcIndex, int length) {
91          throw new ReadOnlyBufferException();
92      }
93  
94      public void setBytes(int index, ByteBuffer src) {
95          throw new ReadOnlyBufferException();
96      }
97  
98      public void setShort(int index, int value) {
99          throw new ReadOnlyBufferException();
100     }
101 
102     public void setMedium(int index, int value) {
103         throw new ReadOnlyBufferException();
104     }
105 
106     public void setInt(int index, int value) {
107         throw new ReadOnlyBufferException();
108     }
109 
110     public void setLong(int index, long value) {
111         throw new ReadOnlyBufferException();
112     }
113 
114     public int setBytes(int index, InputStream in, int length)
115             throws IOException {
116         throw new ReadOnlyBufferException();
117     }
118 
119     public int setBytes(int index, ScatteringByteChannel in, int length)
120             throws IOException {
121         throw new ReadOnlyBufferException();
122     }
123 
124     public int getBytes(int index, GatheringByteChannel out, int length)
125             throws IOException {
126         return buffer.getBytes(index, out, length);
127     }
128 
129     public void getBytes(int index, OutputStream out, int length)
130             throws IOException {
131         buffer.getBytes(index, out, length);
132     }
133 
134     public void getBytes(int index, byte[] dst, int dstIndex, int length) {
135         buffer.getBytes(index, dst, dstIndex, length);
136     }
137 
138     public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
139         buffer.getBytes(index, dst, dstIndex, length);
140     }
141 
142     public void getBytes(int index, ByteBuffer dst) {
143         buffer.getBytes(index, dst);
144     }
145 
146     public ChannelBuffer duplicate() {
147         return new ReadOnlyChannelBuffer(this);
148     }
149 
150     public ChannelBuffer copy(int index, int length) {
151         return buffer.copy(index, length);
152     }
153 
154     public ChannelBuffer slice(int index, int length) {
155         return new ReadOnlyChannelBuffer(buffer.slice(index, length));
156     }
157 
158     public byte getByte(int index) {
159         return buffer.getByte(index);
160     }
161 
162     public short getShort(int index) {
163         return buffer.getShort(index);
164     }
165 
166     public int getUnsignedMedium(int index) {
167         return buffer.getUnsignedMedium(index);
168     }
169 
170     public int getInt(int index) {
171         return buffer.getInt(index);
172     }
173 
174     public long getLong(int index) {
175         return buffer.getLong(index);
176     }
177 
178     public ByteBuffer toByteBuffer(int index, int length) {
179         return buffer.toByteBuffer(index, length).asReadOnlyBuffer();
180     }
181 
182     @Override
183     public ByteBuffer[] toByteBuffers(int index, int length) {
184         ByteBuffer[] bufs = buffer.toByteBuffers(index, length);
185         for (int i = 0; i < bufs.length; i ++) {
186             bufs[i] = bufs[i].asReadOnlyBuffer();
187         }
188         return bufs;
189     }
190 
191     public int capacity() {
192         return buffer.capacity();
193     }
194 }