View Javadoc
1   /*
2    * Copyright 2015 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.lang.invoke.VarHandle;
21  
22  /**
23   * Utility class for heap buffers.
24   */
25  final class HeapByteBufUtil {
26  
27      static byte getByte(byte[] memory, int index) {
28          return memory[index];
29      }
30  
31      static short getShort(byte[] memory, int index) {
32          VarHandle shortBeArrayView = PlatformDependent.shortBeArrayView();
33          if (shortBeArrayView != null) {
34              return (short) shortBeArrayView.get(memory, index);
35          }
36          return getShort0(memory, index);
37      }
38  
39      private static short getShort0(byte[] memory, int index) {
40          return (short) (memory[index] << 8 | memory[index + 1] & 0xFF);
41      }
42  
43      static short getShortLE(byte[] memory, int index) {
44          VarHandle shortLeArrayView = PlatformDependent.shortLeArrayView();
45          if (shortLeArrayView != null) {
46              return (short) shortLeArrayView.get(memory, index);
47          }
48          return (short) (memory[index] & 0xff | memory[index + 1] << 8);
49      }
50  
51      static int getUnsignedMedium(byte[] memory, int index) {
52          return  (memory[index]     & 0xff) << 16 |
53                  (memory[index + 1] & 0xff) <<  8 |
54                  memory[index + 2] & 0xff;
55      }
56  
57      static int getUnsignedMediumLE(byte[] memory, int index) {
58          return  memory[index]     & 0xff         |
59                  (memory[index + 1] & 0xff) <<  8 |
60                  (memory[index + 2] & 0xff) << 16;
61      }
62  
63      static int getInt(byte[] memory, int index) {
64          VarHandle intBeArrayView  = PlatformDependent.intBeArrayView();
65          if (intBeArrayView != null) {
66              return (int) intBeArrayView.get(memory, index);
67          }
68          return getInt0(memory, index);
69      }
70  
71      private static int getInt0(byte[] memory, int index) {
72          return (memory[index] & 0xFF) << 24 |
73                 (memory[index + 1] & 0xFF) << 16 |
74                 (memory[index + 2] & 0xFF) << 8 |
75                 (memory[index + 3] & 0xFF);
76      }
77  
78      static int getIntLE(byte[] memory, int index) {
79          VarHandle intLeArrayView = PlatformDependent.intLeArrayView();
80          if (intLeArrayView != null) {
81              return (int) intLeArrayView.get(memory, index);
82          }
83          return getIntLE0(memory, index);
84      }
85  
86      private static int getIntLE0(byte[] memory, int index) {
87          return (memory[index] & 0xFF) |
88                 (memory[index + 1] & 0xFF) << 8 |
89                 (memory[index + 2] & 0xFF) << 16 |
90                 (memory[index + 3] & 0xFF) << 24;
91      }
92  
93      static long getLong(byte[] memory, int index) {
94          VarHandle longBeArrayView = PlatformDependent.longBeArrayView();
95          if (longBeArrayView != null) {
96              return (long) longBeArrayView.get(memory, index);
97          }
98          return getLong0(memory, index);
99      }
100 
101     private static long getLong0(byte[] memory, int index) {
102         return ((long) memory[index] & 0xFF) << 56 |
103                ((long) memory[index + 1] & 0xFF) << 48 |
104                ((long) memory[index + 2] & 0xFF) << 40 |
105                ((long) memory[index + 3] & 0xFF) << 32 |
106                ((long) memory[index + 4] & 0xFF) << 24 |
107                ((long) memory[index + 5] & 0xFF) << 16 |
108                ((long) memory[index + 6] & 0xFF) << 8 |
109                ((long) memory[index + 7] & 0xFF);
110     }
111 
112     static long getLongLE(byte[] memory, int index) {
113         VarHandle longLeArrayView = PlatformDependent.longLeArrayView();
114         if (longLeArrayView != null) {
115             return (long) longLeArrayView.get(memory, index);
116         }
117         return getLongLE0(memory, index);
118     }
119 
120     private static long getLongLE0(byte[] memory, int index) {
121         return ((long) memory[index] & 0xFF) |
122                ((long) memory[index + 1] & 0xFF) << 8 |
123                ((long) memory[index + 2] & 0xFF) << 16 |
124                ((long) memory[index + 3] & 0xFF) << 24 |
125                ((long) memory[index + 4] & 0xFF) << 32 |
126                ((long) memory[index + 5] & 0xFF) << 40 |
127                ((long) memory[index + 6] & 0xFF) << 48 |
128                ((long) memory[index + 7] & 0xFF) << 56;
129     }
130 
131     static void setByte(byte[] memory, int index, int value) {
132         memory[index] = (byte) value;
133     }
134 
135     static void setShort(byte[] memory, int index, int value) {
136         VarHandle shortBeArrayView = PlatformDependent.shortBeArrayView();
137         if (shortBeArrayView != null) {
138             shortBeArrayView.set(memory, index, (short) value);
139             return;
140         }
141         memory[index]     = (byte) (value >>> 8);
142         memory[index + 1] = (byte) value;
143     }
144 
145     static void setShortLE(byte[] memory, int index, int value) {
146         VarHandle shortLeArrayView = PlatformDependent.shortLeArrayView();
147         if (shortLeArrayView != null) {
148             shortLeArrayView.set(memory, index, (short) value);
149             return;
150         }
151         memory[index]     = (byte) value;
152         memory[index + 1] = (byte) (value >>> 8);
153     }
154 
155     static void setMedium(byte[] memory, int index, int value) {
156         memory[index]     = (byte) (value >>> 16);
157         memory[index + 1] = (byte) (value >>> 8);
158         memory[index + 2] = (byte) value;
159     }
160 
161     static void setMediumLE(byte[] memory, int index, int value) {
162         memory[index]     = (byte) value;
163         memory[index + 1] = (byte) (value >>> 8);
164         memory[index + 2] = (byte) (value >>> 16);
165     }
166 
167     static void setInt(byte[] memory, int index, int value) {
168         VarHandle intBeArrayView = PlatformDependent.intBeArrayView();
169         if (intBeArrayView != null) {
170             intBeArrayView.set(memory, index, value);
171             return;
172         }
173         setInt0(memory, index, value);
174     }
175 
176     private static void setInt0(byte[] memory, int index, int value) {
177         memory[index]     = (byte) (value >>> 24);
178         memory[index + 1] = (byte) (value >>> 16);
179         memory[index + 2] = (byte) (value >>> 8);
180         memory[index + 3] = (byte) value;
181     }
182 
183     static void setIntLE(byte[] memory, int index, int value) {
184         VarHandle intLeArrayView = PlatformDependent.intLeArrayView();
185         if (intLeArrayView != null) {
186             intLeArrayView.set(memory, index, value);
187             return;
188         }
189         setIntLE0(memory, index, value);
190     }
191 
192     private static void setIntLE0(byte[] memory, int index, int value) {
193         memory[index]     = (byte) value;
194         memory[index + 1] = (byte) (value >>> 8);
195         memory[index + 2] = (byte) (value >>> 16);
196         memory[index + 3] = (byte) (value >>> 24);
197     }
198 
199     static void setLong(byte[] memory, int index, long value) {
200         VarHandle longBeArrayView = PlatformDependent.longBeArrayView();
201         if (longBeArrayView != null) {
202             longBeArrayView.set(memory, index, value);
203             return;
204         }
205         setLong0(memory, index, value);
206     }
207 
208     private static void setLong0(byte[] memory, int index, long value) {
209         memory[index]     = (byte) (value >>> 56);
210         memory[index + 1] = (byte) (value >>> 48);
211         memory[index + 2] = (byte) (value >>> 40);
212         memory[index + 3] = (byte) (value >>> 32);
213         memory[index + 4] = (byte) (value >>> 24);
214         memory[index + 5] = (byte) (value >>> 16);
215         memory[index + 6] = (byte) (value >>> 8);
216         memory[index + 7] = (byte) value;
217     }
218 
219     static void setLongLE(byte[] memory, int index, long value) {
220         VarHandle longLeArrayView = PlatformDependent.longLeArrayView();
221         if (longLeArrayView != null) {
222             longLeArrayView.set(memory, index, value);
223             return;
224         }
225         setLongLE0(memory, index, value);
226     }
227 
228     private static void setLongLE0(byte[] memory, int index, long value) {
229         memory[index]     = (byte) value;
230         memory[index + 1] = (byte) (value >>> 8);
231         memory[index + 2] = (byte) (value >>> 16);
232         memory[index + 3] = (byte) (value >>> 24);
233         memory[index + 4] = (byte) (value >>> 32);
234         memory[index + 5] = (byte) (value >>> 40);
235         memory[index + 6] = (byte) (value >>> 48);
236         memory[index + 7] = (byte) (value >>> 56);
237     }
238 
239     private HeapByteBufUtil() { }
240 }