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  /**
19   * Utility class for heap buffers.
20   */
21  final class HeapByteBufUtil {
22  
23      static byte getByte(byte[] memory, int index) {
24          return memory[index];
25      }
26  
27      static short getShort(byte[] memory, int index) {
28          return (short) (memory[index] << 8 | memory[index + 1] & 0xFF);
29      }
30  
31      static short getShortLE(byte[] memory, int index) {
32          return (short) (memory[index] & 0xff | memory[index + 1] << 8);
33      }
34  
35      static int getUnsignedMedium(byte[] memory, int index) {
36          return  (memory[index]     & 0xff) << 16 |
37                  (memory[index + 1] & 0xff) <<  8 |
38                  memory[index + 2] & 0xff;
39      }
40  
41      static int getUnsignedMediumLE(byte[] memory, int index) {
42          return  memory[index]     & 0xff         |
43                  (memory[index + 1] & 0xff) <<  8 |
44                  (memory[index + 2] & 0xff) << 16;
45      }
46  
47      static int getInt(byte[] memory, int index) {
48          return  (memory[index]     & 0xff) << 24 |
49                  (memory[index + 1] & 0xff) << 16 |
50                  (memory[index + 2] & 0xff) <<  8 |
51                  memory[index + 3] & 0xff;
52      }
53  
54      static int getIntLE(byte[] memory, int index) {
55          return  memory[index]      & 0xff        |
56                  (memory[index + 1] & 0xff) << 8  |
57                  (memory[index + 2] & 0xff) << 16 |
58                  (memory[index + 3] & 0xff) << 24;
59      }
60  
61      static long getLong(byte[] memory, int index) {
62          return  ((long) memory[index]     & 0xff) << 56 |
63                  ((long) memory[index + 1] & 0xff) << 48 |
64                  ((long) memory[index + 2] & 0xff) << 40 |
65                  ((long) memory[index + 3] & 0xff) << 32 |
66                  ((long) memory[index + 4] & 0xff) << 24 |
67                  ((long) memory[index + 5] & 0xff) << 16 |
68                  ((long) memory[index + 6] & 0xff) <<  8 |
69                  (long) memory[index + 7] & 0xff;
70      }
71  
72      static long getLongLE(byte[] memory, int index) {
73          return  (long) memory[index]      & 0xff        |
74                  ((long) memory[index + 1] & 0xff) <<  8 |
75                  ((long) memory[index + 2] & 0xff) << 16 |
76                  ((long) memory[index + 3] & 0xff) << 24 |
77                  ((long) memory[index + 4] & 0xff) << 32 |
78                  ((long) memory[index + 5] & 0xff) << 40 |
79                  ((long) memory[index + 6] & 0xff) << 48 |
80                  ((long) memory[index + 7] & 0xff) << 56;
81      }
82  
83      static void setByte(byte[] memory, int index, int value) {
84          memory[index] = (byte) value;
85      }
86  
87      static void setShort(byte[] memory, int index, int value) {
88          memory[index]     = (byte) (value >>> 8);
89          memory[index + 1] = (byte) value;
90      }
91  
92      static void setShortLE(byte[] memory, int index, int value) {
93          memory[index]     = (byte) value;
94          memory[index + 1] = (byte) (value >>> 8);
95      }
96  
97      static void setMedium(byte[] memory, int index, int value) {
98          memory[index]     = (byte) (value >>> 16);
99          memory[index + 1] = (byte) (value >>> 8);
100         memory[index + 2] = (byte) value;
101     }
102 
103     static void setMediumLE(byte[] memory, int index, int value) {
104         memory[index]     = (byte) value;
105         memory[index + 1] = (byte) (value >>> 8);
106         memory[index + 2] = (byte) (value >>> 16);
107     }
108 
109     static void setInt(byte[] memory, int index, int value) {
110         memory[index]     = (byte) (value >>> 24);
111         memory[index + 1] = (byte) (value >>> 16);
112         memory[index + 2] = (byte) (value >>> 8);
113         memory[index + 3] = (byte) value;
114     }
115 
116     static void setIntLE(byte[] memory, int index, int value) {
117         memory[index]     = (byte) value;
118         memory[index + 1] = (byte) (value >>> 8);
119         memory[index + 2] = (byte) (value >>> 16);
120         memory[index + 3] = (byte) (value >>> 24);
121     }
122 
123     static void setLong(byte[] memory, int index, long value) {
124         memory[index]     = (byte) (value >>> 56);
125         memory[index + 1] = (byte) (value >>> 48);
126         memory[index + 2] = (byte) (value >>> 40);
127         memory[index + 3] = (byte) (value >>> 32);
128         memory[index + 4] = (byte) (value >>> 24);
129         memory[index + 5] = (byte) (value >>> 16);
130         memory[index + 6] = (byte) (value >>> 8);
131         memory[index + 7] = (byte) value;
132     }
133 
134     static void setLongLE(byte[] memory, int index, long value) {
135         memory[index]     = (byte) value;
136         memory[index + 1] = (byte) (value >>> 8);
137         memory[index + 2] = (byte) (value >>> 16);
138         memory[index + 3] = (byte) (value >>> 24);
139         memory[index + 4] = (byte) (value >>> 32);
140         memory[index + 5] = (byte) (value >>> 40);
141         memory[index + 6] = (byte) (value >>> 48);
142         memory[index + 7] = (byte) (value >>> 56);
143     }
144 
145     private HeapByteBufUtil() { }
146 }