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