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