1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
27
28 final class ReadOnlyUnsafeDirectByteBuf extends ReadOnlyByteBufferBuf {
29 private final long memoryAddress;
30
31 ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer byteBuffer) {
32 super(allocator, byteBuffer);
33
34
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 protected ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length, boolean internal) {
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 protected ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) {
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 }