查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   https://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.ObjectUtil;
20  import io.netty.util.internal.PlatformDependent;
21  
22  import java.nio.ByteBuffer;
23  
24  
25  /**
26   * Read-only ByteBuf which wraps a read-only direct ByteBuffer and use unsafe for best performance.
27   */
28  final class ReadOnlyUnsafeDirectByteBuf extends ReadOnlyByteBufferBuf {
29      private final long memoryAddress;
30  
31      ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer byteBuffer) {
32          super(allocator, byteBuffer);
33          // Use buffer as the super class will slice the passed in ByteBuffer which means the memoryAddress
34          // may be different if the position != 0.
35          memoryAddress = PlatformDependent.directBufferAddress(buffer);
36      }
37  
38      @Override
39      protected byte _getByte(int index) {
40          return UnsafeByteBufUtil.getByte(addr(index));
41      }
42  
43      @Override
44      protected short _getShort(int index) {
45          return UnsafeByteBufUtil.getShort(addr(index));
46      }
47  
48      @Override
49      protected int _getUnsignedMedium(int index) {
50          return UnsafeByteBufUtil.getUnsignedMedium(addr(index));
51      }
52  
53      @Override
54      protected int _getInt(int index) {
55          return UnsafeByteBufUtil.getInt(addr(index));
56      }
57  
58      @Override
59      protected long _getLong(int index) {
60          return UnsafeByteBufUtil.getLong(addr(index));
61      }
62  
63      @Override
64      public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
65          checkIndex(index, length);
66          ObjectUtil.checkNotNull(dst, "dst");
67          if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
68              throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
69          }
70  
71          if (dst.hasMemoryAddress()) {
72              PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
73          } else if (dst.hasArray()) {
74              PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
75          } else {
76              dst.setBytes(dstIndex, this, index, length);
77          }
78          return this;
79      }
80  
81      @Override
82      public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
83          checkIndex(index, length);
84          ObjectUtil.checkNotNull(dst, "dst");
85          if (dstIndex < 0 || dstIndex > dst.length - length) {
86              throw new IndexOutOfBoundsException(String.format(
87                      "dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
88          }
89  
90          if (length != 0) {
91              PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
92          }
93          return this;
94      }
95  
96      @Override
97      public ByteBuf copy(int index, int length) {
98          checkIndex(index, length);
99          ByteBuf copy = alloc().directBuffer(length, maxCapacity());
100         if (length != 0) {
101             if (copy.hasMemoryAddress()) {
102                 PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
103                 copy.setIndex(0, length);
104             } else {
105                 copy.writeBytes(this, index, length);
106             }
107         }
108         return copy;
109     }
110 
111     @Override
112     public boolean hasMemoryAddress() {
113         return true;
114     }
115 
116     @Override
117     public long memoryAddress() {
118         return memoryAddress;
119     }
120 
121     private long addr(int index) {
122         return memoryAddress + index;
123     }
124 }