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  
19  import io.netty.util.internal.PlatformDependent;
20  
21  import java.nio.ByteBuffer;
22  
23  
24  /**
25   * Read-only ByteBuf which wraps a read-only direct ByteBuffer and use unsafe for best performance.
26   */
27  final class ReadOnlyUnsafeDirectByteBuf extends ReadOnlyByteBufferBuf {
28      private final long memoryAddress;
29  
30      ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
31          super(allocator, buffer);
32          memoryAddress = PlatformDependent.directBufferAddress(buffer);
33      }
34  
35      @Override
36      protected byte _getByte(int index) {
37          return UnsafeByteBufUtil.getByte(addr(index));
38      }
39  
40      @Override
41      protected short _getShort(int index) {
42          return UnsafeByteBufUtil.getShort(addr(index));
43      }
44  
45      @Override
46      protected int _getUnsignedMedium(int index) {
47          return UnsafeByteBufUtil.getUnsignedMedium(addr(index));
48      }
49  
50      @Override
51      protected int _getInt(int index) {
52          return UnsafeByteBufUtil.getInt(addr(index));
53      }
54  
55      @Override
56      protected long _getLong(int index) {
57          return UnsafeByteBufUtil.getLong(addr(index));
58      }
59  
60      @Override
61      public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
62          checkIndex(index, length);
63          if (dst == null) {
64              throw new NullPointerException("dst");
65          }
66          if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
67              throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
68          }
69  
70          if (dst.hasMemoryAddress()) {
71              PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
72          } else if (dst.hasArray()) {
73              PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
74          } else {
75              dst.setBytes(dstIndex, this, index, length);
76          }
77          return this;
78      }
79  
80      @Override
81      public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
82          checkIndex(index, length);
83          if (dst == null) {
84              throw new NullPointerException("dst");
85          }
86          if (dstIndex < 0 || dstIndex > dst.length - length) {
87              throw new IndexOutOfBoundsException(String.format(
88                      "dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
89          }
90  
91          if (length != 0) {
92              PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
93          }
94          return this;
95      }
96  
97      @Override
98      public ByteBuf getBytes(int index, ByteBuffer dst) {
99          checkIndex(index);
100         if (dst == null) {
101             throw new NullPointerException("dst");
102         }
103 
104         int bytesToCopy = Math.min(capacity() - index, dst.remaining());
105         ByteBuffer tmpBuf = internalNioBuffer();
106         tmpBuf.clear().position(index).limit(index + bytesToCopy);
107         dst.put(tmpBuf);
108         return this;
109     }
110 
111     @Override
112     public ByteBuf copy(int index, int length) {
113         checkIndex(index, length);
114         ByteBuf copy = alloc().directBuffer(length, maxCapacity());
115         if (length != 0) {
116             if (copy.hasMemoryAddress()) {
117                 PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
118                 copy.setIndex(0, length);
119             } else {
120                 copy.writeBytes(this, index, length);
121             }
122         }
123         return copy;
124     }
125 
126     private long addr(int index) {
127         return memoryAddress + index;
128     }
129 }