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    *   http://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 int getUnsignedMedium(byte[] memory, int index) {
32          return  (memory[index]     & 0xff) << 16 |
33                  (memory[index + 1] & 0xff) <<  8 |
34                  memory[index + 2] & 0xff;
35      }
36  
37      static int getInt(byte[] memory, int index) {
38          return  (memory[index]     & 0xff) << 24 |
39                  (memory[index + 1] & 0xff) << 16 |
40                  (memory[index + 2] & 0xff) <<  8 |
41                  memory[index + 3] & 0xff;
42      }
43  
44      static long getLong(byte[] memory, int index) {
45          return  ((long) memory[index]     & 0xff) << 56 |
46                  ((long) memory[index + 1] & 0xff) << 48 |
47                  ((long) memory[index + 2] & 0xff) << 40 |
48                  ((long) memory[index + 3] & 0xff) << 32 |
49                  ((long) memory[index + 4] & 0xff) << 24 |
50                  ((long) memory[index + 5] & 0xff) << 16 |
51                  ((long) memory[index + 6] & 0xff) <<  8 |
52                  (long) memory[index + 7] & 0xff;
53      }
54  
55      static void setByte(byte[] memory, int index, int value) {
56          memory[index] = (byte) value;
57      }
58  
59      static void setShort(byte[] memory, int index, int value) {
60          memory[index]     = (byte) (value >>> 8);
61          memory[index + 1] = (byte) value;
62      }
63  
64      static void setMedium(byte[] memory, int index, int value) {
65          memory[index]     = (byte) (value >>> 16);
66          memory[index + 1] = (byte) (value >>> 8);
67          memory[index + 2] = (byte) value;
68      }
69  
70      static void setInt(byte[] memory, int index, int value) {
71          memory[index]     = (byte) (value >>> 24);
72          memory[index + 1] = (byte) (value >>> 16);
73          memory[index + 2] = (byte) (value >>> 8);
74          memory[index + 3] = (byte) value;
75      }
76  
77      static void setLong(byte[] memory, int index, long value) {
78          memory[index]     = (byte) (value >>> 56);
79          memory[index + 1] = (byte) (value >>> 48);
80          memory[index + 2] = (byte) (value >>> 40);
81          memory[index + 3] = (byte) (value >>> 32);
82          memory[index + 4] = (byte) (value >>> 24);
83          memory[index + 5] = (byte) (value >>> 16);
84          memory[index + 6] = (byte) (value >>> 8);
85          memory[index + 7] = (byte) value;
86      }
87  
88      private HeapByteBufUtil() { }
89  }