View Javadoc
1   /*
2    * Copyright 2013 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 io.netty.util.internal.StringUtil;
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.GatheringByteChannel;
27  import java.nio.channels.ScatteringByteChannel;
28  
29  
30  /**
31   * Read-only ByteBuf which wraps a read-only ByteBuffer.
32   */
33  class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
34  
35      protected final ByteBuffer buffer;
36      private final ByteBufAllocator allocator;
37      private ByteBuffer tmpNioBuf;
38  
39      ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
40          super(buffer.remaining());
41          if (!buffer.isReadOnly()) {
42              throw new IllegalArgumentException("must be a readonly buffer: " + StringUtil.simpleClassName(buffer));
43          }
44  
45          this.allocator = allocator;
46          this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN);
47          writerIndex(this.buffer.limit());
48      }
49  
50      @Override
51      protected void deallocate() { }
52  
53      @Override
54      public byte getByte(int index) {
55          ensureAccessible();
56          return _getByte(index);
57      }
58  
59      @Override
60      protected byte _getByte(int index) {
61          return buffer.get(index);
62      }
63  
64      @Override
65      public short getShort(int index) {
66          ensureAccessible();
67          return _getShort(index);
68      }
69  
70      @Override
71      protected short _getShort(int index) {
72          return buffer.getShort(index);
73      }
74  
75      @Override
76      public int getUnsignedMedium(int index) {
77          ensureAccessible();
78          return _getUnsignedMedium(index);
79      }
80  
81      @Override
82      protected int _getUnsignedMedium(int index) {
83          return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff;
84      }
85  
86      @Override
87      public int getInt(int index) {
88          ensureAccessible();
89          return _getInt(index);
90      }
91  
92      @Override
93      protected int _getInt(int index) {
94          return buffer.getInt(index);
95      }
96  
97      @Override
98      public long getLong(int index) {
99          ensureAccessible();
100         return _getLong(index);
101     }
102 
103     @Override
104     protected long _getLong(int index) {
105         return buffer.getLong(index);
106     }
107 
108     @Override
109     public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
110         checkDstIndex(index, length, dstIndex, dst.capacity());
111         if (dst.hasArray()) {
112             getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
113         } else if (dst.nioBufferCount() > 0) {
114             for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) {
115                 int bbLen = bb.remaining();
116                 getBytes(index, bb);
117                 index += bbLen;
118             }
119         } else {
120             dst.setBytes(dstIndex, this, index, length);
121         }
122         return this;
123     }
124 
125     @Override
126     public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
127         checkDstIndex(index, length, dstIndex, dst.length);
128 
129         if (dstIndex < 0 || dstIndex > dst.length - length) {
130             throw new IndexOutOfBoundsException(String.format(
131                     "dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
132         }
133 
134         ByteBuffer tmpBuf = internalNioBuffer();
135         tmpBuf.clear().position(index).limit(index + length);
136         tmpBuf.get(dst, dstIndex, length);
137         return this;
138     }
139 
140     @Override
141     public ByteBuf getBytes(int index, ByteBuffer dst) {
142         checkIndex(index);
143         if (dst == null) {
144             throw new NullPointerException("dst");
145         }
146 
147         int bytesToCopy = Math.min(capacity() - index, dst.remaining());
148         ByteBuffer tmpBuf = internalNioBuffer();
149         tmpBuf.clear().position(index).limit(index + bytesToCopy);
150         dst.put(tmpBuf);
151         return this;
152     }
153 
154     @Override
155     protected void _setByte(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     protected void _setMedium(int index, int value) {
166         throw new ReadOnlyBufferException();
167     }
168 
169     @Override
170     protected void _setInt(int index, int value) {
171         throw new ReadOnlyBufferException();
172     }
173 
174     @Override
175     protected void _setLong(int index, long value) {
176         throw new ReadOnlyBufferException();
177     }
178 
179     @Override
180     public int capacity() {
181         return maxCapacity();
182     }
183 
184     @Override
185     public ByteBuf capacity(int newCapacity) {
186         throw new ReadOnlyBufferException();
187     }
188 
189     @Override
190     public ByteBufAllocator alloc() {
191         return allocator;
192     }
193 
194     @Override
195     public ByteOrder order() {
196         return ByteOrder.BIG_ENDIAN;
197     }
198 
199     @Override
200     public ByteBuf unwrap() {
201         return null;
202     }
203 
204     @Override
205     public boolean isDirect() {
206         return buffer.isDirect();
207     }
208 
209     @Override
210     public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
211         ensureAccessible();
212         if (length == 0) {
213             return this;
214         }
215 
216         if (buffer.hasArray()) {
217             out.write(buffer.array(), index + buffer.arrayOffset(), length);
218         } else {
219             byte[] tmp = new byte[length];
220             ByteBuffer tmpBuf = internalNioBuffer();
221             tmpBuf.clear().position(index);
222             tmpBuf.get(tmp);
223             out.write(tmp);
224         }
225         return this;
226     }
227 
228     @Override
229     public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
230         ensureAccessible();
231         if (length == 0) {
232             return 0;
233         }
234 
235         ByteBuffer tmpBuf = internalNioBuffer();
236         tmpBuf.clear().position(index).limit(index + length);
237         return out.write(tmpBuf);
238     }
239 
240     @Override
241     public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
242         throw new ReadOnlyBufferException();
243     }
244 
245     @Override
246     public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
247         throw new ReadOnlyBufferException();
248     }
249 
250     @Override
251     public ByteBuf setBytes(int index, ByteBuffer src) {
252         throw new ReadOnlyBufferException();
253     }
254 
255     @Override
256     public int setBytes(int index, InputStream in, int length) throws IOException {
257         throw new ReadOnlyBufferException();
258     }
259 
260     @Override
261     public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
262         throw new ReadOnlyBufferException();
263     }
264 
265     protected final ByteBuffer internalNioBuffer() {
266         ByteBuffer tmpNioBuf = this.tmpNioBuf;
267         if (tmpNioBuf == null) {
268             this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
269         }
270         return tmpNioBuf;
271     }
272 
273     @Override
274     public ByteBuf copy(int index, int length) {
275         ensureAccessible();
276         ByteBuffer src;
277         try {
278             src = (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
279         } catch (IllegalArgumentException ignored) {
280             throw new IndexOutOfBoundsException("Too many bytes to read - Need " + (index + length));
281         }
282 
283         ByteBuffer dst = ByteBuffer.allocateDirect(length);
284         dst.put(src);
285         dst.order(order());
286         dst.clear();
287         return new UnpooledDirectByteBuf(alloc(), dst, maxCapacity());
288     }
289 
290     @Override
291     public int nioBufferCount() {
292         return 1;
293     }
294 
295     @Override
296     public ByteBuffer[] nioBuffers(int index, int length) {
297         return new ByteBuffer[] { nioBuffer(index, length) };
298     }
299 
300     @Override
301     public ByteBuffer nioBuffer(int index, int length) {
302         return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
303     }
304 
305     @Override
306     public ByteBuffer internalNioBuffer(int index, int length) {
307         ensureAccessible();
308         return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
309     }
310 
311     @Override
312     public boolean hasArray() {
313         return buffer.hasArray();
314     }
315 
316     @Override
317     public byte[] array() {
318         return buffer.array();
319     }
320 
321     @Override
322     public int arrayOffset() {
323         return buffer.arrayOffset();
324     }
325 
326     @Override
327     public boolean hasMemoryAddress() {
328         return false;
329     }
330 
331     @Override
332     public long memoryAddress() {
333         throw new UnsupportedOperationException();
334     }
335 }