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 byteBuffer) {
31          super(allocator, byteBuffer);
32          // Use buffer as the super class will slice the passed in ByteBuffer which means the memoryAddress
33          // may be different if the position != 0.
34          memoryAddress = PlatformDependent.directBufferAddress(buffer);
35      }
36  
37      @Override
38      protected byte _getByte(int index) {
39          return UnsafeByteBufUtil.getByte(addr(index));
40      }
41  
42      @Override
43      protected short _getShort(int index) {
44          return UnsafeByteBufUtil.getShort(addr(index));
45      }
46  
47      @Override
48      protected int _getUnsignedMedium(int index) {
49          return UnsafeByteBufUtil.getUnsignedMedium(addr(index));
50      }
51  
52      @Override
53      protected int _getInt(int index) {
54          return UnsafeByteBufUtil.getInt(addr(index));
55      }
56  
57      @Override
58      protected long _getLong(int index) {
59          return UnsafeByteBufUtil.getLong(addr(index));
60      }
61  
62      @Override
63      public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
64          checkIndex(index, length);
65          if (dst == null) {
66              throw new NullPointerException("dst");
67          }
68          if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
69              throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
70          }
71  
72          if (dst.hasMemoryAddress()) {
73              PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
74          } else if (dst.hasArray()) {
75              PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
76          } else {
77              dst.setBytes(dstIndex, this, index, length);
78          }
79          return this;
80      }
81  
82      @Override
83      public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
84          checkIndex(index, length);
85          if (dst == null) {
86              throw new NullPointerException("dst");
87          }
88          if (dstIndex < 0 || dstIndex > dst.length - length) {
89              throw new IndexOutOfBoundsException(String.format(
90                      "dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
91          }
92  
93          if (length != 0) {
94              PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
95          }
96          return this;
97      }
98  
99      @Override
100     public ByteBuf getBytes(int index, ByteBuffer dst) {
101         checkIndex(index);
102         if (dst == null) {
103             throw new NullPointerException("dst");
104         }
105 
106         int bytesToCopy = Math.min(capacity() - index, dst.remaining());
107         ByteBuffer tmpBuf = internalNioBuffer();
108         tmpBuf.clear().position(index).limit(index + bytesToCopy);
109         dst.put(tmpBuf);
110         return this;
111     }
112 
113     @Override
114     public ByteBuf copy(int index, int length) {
115         checkIndex(index, length);
116         ByteBuf copy = alloc().directBuffer(length, maxCapacity());
117         if (length != 0) {
118             if (copy.hasMemoryAddress()) {
119                 PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
120                 copy.setIndex(0, length);
121             } else {
122                 copy.writeBytes(this, index, length);
123             }
124         }
125         return copy;
126     }
127 
128     @Override
129     public boolean hasMemoryAddress() {
130         return true;
131     }
132 
133     @Override
134     public long memoryAddress() {
135         return memoryAddress;
136     }
137 
138     private long addr(int index) {
139         return memoryAddress + index;
140     }
141 }