View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
33   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
34   *
35   * @version $Rev: 2206 $, $Date: 2010-03-03 14:35:01 +0900 (Wed, 03 Mar 2010) $
36   *
37   */
38  public class ReadOnlyChannelBuffer extends AbstractChannelBuffer implements WrappedChannelBuffer {
39  
40      private final ChannelBuffer buffer;
41  
42      public ReadOnlyChannelBuffer(ChannelBuffer buffer) {
43          if (buffer == null) {
44              throw new NullPointerException("buffer");
45          }
46          this.buffer = buffer;
47          setIndex(buffer.readerIndex(), buffer.writerIndex());
48      }
49  
50      private ReadOnlyChannelBuffer(ReadOnlyChannelBuffer buffer) {
51          this.buffer = buffer.buffer;
52          setIndex(buffer.readerIndex(), buffer.writerIndex());
53      }
54  
55      public ChannelBuffer unwrap() {
56          return buffer;
57      }
58  
59      public ChannelBufferFactory factory() {
60          return buffer.factory();
61      }
62  
63      public ByteOrder order() {
64          return buffer.order();
65      }
66  
67      public boolean isDirect() {
68          return buffer.isDirect();
69      }
70  
71      public boolean hasArray() {
72          return false;
73      }
74  
75      public byte[] array() {
76          throw new ReadOnlyBufferException();
77      }
78  
79      public int arrayOffset() {
80          throw new ReadOnlyBufferException();
81      }
82  
83      @Override
84      public void discardReadBytes() {
85          throw new ReadOnlyBufferException();
86      }
87  
88      public void setByte(int index, int value) {
89          throw new ReadOnlyBufferException();
90      }
91  
92      public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
93          throw new ReadOnlyBufferException();
94      }
95  
96      public void setBytes(int index, byte[] src, int srcIndex, int length) {
97          throw new ReadOnlyBufferException();
98      }
99  
100     public void setBytes(int index, ByteBuffer src) {
101         throw new ReadOnlyBufferException();
102     }
103 
104     public void setShort(int index, int value) {
105         throw new ReadOnlyBufferException();
106     }
107 
108     public void setMedium(int index, int value) {
109         throw new ReadOnlyBufferException();
110     }
111 
112     public void setInt(int index, int value) {
113         throw new ReadOnlyBufferException();
114     }
115 
116     public void setLong(int index, long value) {
117         throw new ReadOnlyBufferException();
118     }
119 
120     public int setBytes(int index, InputStream in, int length)
121             throws IOException {
122         throw new ReadOnlyBufferException();
123     }
124 
125     public int setBytes(int index, ScatteringByteChannel in, int length)
126             throws IOException {
127         throw new ReadOnlyBufferException();
128     }
129 
130     public int getBytes(int index, GatheringByteChannel out, int length)
131             throws IOException {
132         return buffer.getBytes(index, out, length);
133     }
134 
135     public void getBytes(int index, OutputStream out, int length)
136             throws IOException {
137         buffer.getBytes(index, out, length);
138     }
139 
140     public void getBytes(int index, byte[] dst, int dstIndex, int length) {
141         buffer.getBytes(index, dst, dstIndex, length);
142     }
143 
144     public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
145         buffer.getBytes(index, dst, dstIndex, length);
146     }
147 
148     public void getBytes(int index, ByteBuffer dst) {
149         buffer.getBytes(index, dst);
150     }
151 
152     public ChannelBuffer duplicate() {
153         return new ReadOnlyChannelBuffer(this);
154     }
155 
156     public ChannelBuffer copy(int index, int length) {
157         return buffer.copy(index, length);
158     }
159 
160     public ChannelBuffer slice(int index, int length) {
161         return new ReadOnlyChannelBuffer(buffer.slice(index, length));
162     }
163 
164     public byte getByte(int index) {
165         return buffer.getByte(index);
166     }
167 
168     public short getShort(int index) {
169         return buffer.getShort(index);
170     }
171 
172     public int getUnsignedMedium(int index) {
173         return buffer.getUnsignedMedium(index);
174     }
175 
176     public int getInt(int index) {
177         return buffer.getInt(index);
178     }
179 
180     public long getLong(int index) {
181         return buffer.getLong(index);
182     }
183 
184     public ByteBuffer toByteBuffer(int index, int length) {
185         return buffer.toByteBuffer(index, length).asReadOnlyBuffer();
186     }
187 
188     @Override
189     public ByteBuffer[] toByteBuffers(int index, int length) {
190         ByteBuffer[] bufs = buffer.toByteBuffers(index, length);
191         for (int i = 0; i < bufs.length; i ++) {
192             bufs[i] = bufs[i].asReadOnlyBuffer();
193         }
194         return bufs;
195     }
196 
197     public int capacity() {
198         return buffer.capacity();
199     }
200 }