View Javadoc
1   /*
2    * Copyright 2025 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  import io.netty.util.internal.PlatformDependent;
19  
20  import java.nio.ByteBuffer;
21  
22  /**
23   * Centralizes all ByteBuffer VarHandle get/set calls so classes like UnpooledDirectByteBuf
24   * don't directly reference signature-polymorphic methods. This allows avoiding class verification
25   * failures on older Android runtimes by not loading this class when VarHandle is disabled.
26   *
27   * Methods here must only be called when PlatformDependent.hasVarHandle() is true.
28   */
29  final class VarHandleByteBufferAccess {
30  
31      private VarHandleByteBufferAccess() {
32      }
33  
34      // short (big endian)
35      static short getShortBE(ByteBuffer buffer, int index) {
36          //noinspection DataFlowIssue
37          return (short) PlatformDependent.shortBeByteBufferView().get(buffer, index);
38      }
39  
40      static void setShortBE(ByteBuffer buffer, int index, int value) {
41          //noinspection DataFlowIssue
42          PlatformDependent.shortBeByteBufferView().set(buffer, index, (short) value);
43      }
44  
45      // short (little endian)
46      static short getShortLE(ByteBuffer buffer, int index) {
47          //noinspection DataFlowIssue
48          return (short) PlatformDependent.shortLeByteBufferView().get(buffer, index);
49      }
50  
51      static void setShortLE(ByteBuffer buffer, int index, int value) {
52          //noinspection DataFlowIssue
53          PlatformDependent.shortLeByteBufferView().set(buffer, index, (short) value);
54      }
55  
56      // int (big endian)
57      static int getIntBE(ByteBuffer buffer, int index) {
58          //noinspection DataFlowIssue
59          return (int) PlatformDependent.intBeByteBufferView().get(buffer, index);
60      }
61  
62      static void setIntBE(ByteBuffer buffer, int index, int value) {
63          //noinspection DataFlowIssue
64          PlatformDependent.intBeByteBufferView().set(buffer, index, value);
65      }
66  
67      // int (little endian)
68      static int getIntLE(ByteBuffer buffer, int index) {
69          //noinspection DataFlowIssue
70          return (int) PlatformDependent.intLeByteBufferView().get(buffer, index);
71      }
72  
73      static void setIntLE(ByteBuffer buffer, int index, int value) {
74          //noinspection DataFlowIssue
75          PlatformDependent.intLeByteBufferView().set(buffer, index, value);
76      }
77  
78      // long (big endian)
79      static long getLongBE(ByteBuffer buffer, int index) {
80          //noinspection DataFlowIssue
81          return (long) PlatformDependent.longBeByteBufferView().get(buffer, index);
82      }
83  
84      static void setLongBE(ByteBuffer buffer, int index, long value) {
85          //noinspection DataFlowIssue
86          PlatformDependent.longBeByteBufferView().set(buffer, index, value);
87      }
88  
89      // long (little endian)
90      static long getLongLE(ByteBuffer buffer, int index) {
91          //noinspection DataFlowIssue
92          return (long) PlatformDependent.longLeByteBufferView().get(buffer, index);
93      }
94  
95      static void setLongLE(ByteBuffer buffer, int index, long value) {
96          //noinspection DataFlowIssue
97          PlatformDependent.longLeByteBufferView().set(buffer, index, value);
98      }
99  
100     // --------------------------------------------------------------------
101     // byte[] (heap array) accessors
102     // These centralize VarHandle.get/set calls for heap arrays as well,
103     // so classes like HeapByteBufUtil do not call signature-polymorphic
104     // methods directly.
105     // Methods must only be called when PlatformDependent.hasVarHandle() is true.
106     // --------------------------------------------------------------------
107 
108     // short (big endian)
109     static short getShortBE(byte[] memory, int index) {
110         //noinspection DataFlowIssue
111         return (short) PlatformDependent.shortBeArrayView().get(memory, index);
112     }
113 
114     static void setShortBE(byte[] memory, int index, int value) {
115         //noinspection DataFlowIssue
116         PlatformDependent.shortBeArrayView().set(memory, index, (short) value);
117     }
118 
119     // short (little endian)
120     static short getShortLE(byte[] memory, int index) {
121         //noinspection DataFlowIssue
122         return (short) PlatformDependent.shortLeArrayView().get(memory, index);
123     }
124 
125     static void setShortLE(byte[] memory, int index, int value) {
126         //noinspection DataFlowIssue
127         PlatformDependent.shortLeArrayView().set(memory, index, (short) value);
128     }
129 
130     // int (big endian)
131     static int getIntBE(byte[] memory, int index) {
132         //noinspection DataFlowIssue
133         return (int) PlatformDependent.intBeArrayView().get(memory, index);
134     }
135 
136     static void setIntBE(byte[] memory, int index, int value) {
137         //noinspection DataFlowIssue
138         PlatformDependent.intBeArrayView().set(memory, index, value);
139     }
140 
141     // int (little endian)
142     static int getIntLE(byte[] memory, int index) {
143         //noinspection DataFlowIssue
144         return (int) PlatformDependent.intLeArrayView().get(memory, index);
145     }
146 
147     static void setIntLE(byte[] memory, int index, int value) {
148         //noinspection DataFlowIssue
149         PlatformDependent.intLeArrayView().set(memory, index, value);
150     }
151 
152     // long (big endian)
153     static long getLongBE(byte[] memory, int index) {
154         //noinspection DataFlowIssue
155         return (long) PlatformDependent.longBeArrayView().get(memory, index);
156     }
157 
158     static void setLongBE(byte[] memory, int index, long value) {
159         //noinspection DataFlowIssue
160         PlatformDependent.longBeArrayView().set(memory, index, value);
161     }
162 
163     // long (little endian)
164     static long getLongLE(byte[] memory, int index) {
165         //noinspection DataFlowIssue
166         return (long) PlatformDependent.longLeArrayView().get(memory, index);
167     }
168 
169     static void setLongLE(byte[] memory, int index, long value) {
170         //noinspection DataFlowIssue
171         PlatformDependent.longLeArrayView().set(memory, index, value);
172     }
173 }