/
ByteConverter.java
197 lines (181 loc) · 5.57 KB
/
ByteConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) 2011, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.util;
/**
* Helper methods to parse java base types from byte arrays.
*
* @author Mikko Tiihonen
*/
public class ByteConverter {
private ByteConverter() {
// prevent instantiation of static helper class
}
/**
* Convert a variable length array of bytes to an integer
* @param bytes array of bytes that can be decoded as an integer
* @return integer
*/
public static int bytesToInt(byte []bytes) {
if ( bytes.length == 1 ) {
return (int)bytes[0];
}
if ( bytes.length == 2 ) {
return int2(bytes, 0);
}
if ( bytes.length == 4 ) {
return int4(bytes, 0);
} else {
throw new IllegalArgumentException("Argument bytes is empty");
}
}
/**
* Parses a long value from the byte array.
*
* @param bytes The byte array to parse.
* @param idx The starting index of the parse in the byte array.
* @return parsed long value.
*/
public static long int8(byte[] bytes, int idx) {
return
((long) (bytes[idx + 0] & 255) << 56)
+ ((long) (bytes[idx + 1] & 255) << 48)
+ ((long) (bytes[idx + 2] & 255) << 40)
+ ((long) (bytes[idx + 3] & 255) << 32)
+ ((long) (bytes[idx + 4] & 255) << 24)
+ ((long) (bytes[idx + 5] & 255) << 16)
+ ((long) (bytes[idx + 6] & 255) << 8)
+ (bytes[idx + 7] & 255);
}
/**
* Parses an int value from the byte array.
*
* @param bytes The byte array to parse.
* @param idx The starting index of the parse in the byte array.
* @return parsed int value.
*/
public static int int4(byte[] bytes, int idx) {
return
((bytes[idx] & 255) << 24)
+ ((bytes[idx + 1] & 255) << 16)
+ ((bytes[idx + 2] & 255) << 8)
+ ((bytes[idx + 3] & 255));
}
/**
* Parses a short value from the byte array.
*
* @param bytes The byte array to parse.
* @param idx The starting index of the parse in the byte array.
* @return parsed short value.
*/
public static short int2(byte[] bytes, int idx) {
return (short) (((bytes[idx] & 255) << 8) + ((bytes[idx + 1] & 255)));
}
/**
* Parses a boolean value from the byte array.
*
* @param bytes
* The byte array to parse.
* @param idx
* The starting index to read from bytes.
* @return parsed boolean value.
*/
public static boolean bool(byte[] bytes, int idx) {
return bytes[idx] == 1;
}
/**
* Parses a float value from the byte array.
*
* @param bytes The byte array to parse.
* @param idx The starting index of the parse in the byte array.
* @return parsed float value.
*/
public static float float4(byte[] bytes, int idx) {
return Float.intBitsToFloat(int4(bytes, idx));
}
/**
* Parses a double value from the byte array.
*
* @param bytes The byte array to parse.
* @param idx The starting index of the parse in the byte array.
* @return parsed double value.
*/
public static double float8(byte[] bytes, int idx) {
return Double.longBitsToDouble(int8(bytes, idx));
}
/**
* Encodes a long value to the byte array.
*
* @param target The byte array to encode to.
* @param idx The starting index in the byte array.
* @param value The value to encode.
*/
public static void int8(byte[] target, int idx, long value) {
target[idx + 0] = (byte) (value >>> 56);
target[idx + 1] = (byte) (value >>> 48);
target[idx + 2] = (byte) (value >>> 40);
target[idx + 3] = (byte) (value >>> 32);
target[idx + 4] = (byte) (value >>> 24);
target[idx + 5] = (byte) (value >>> 16);
target[idx + 6] = (byte) (value >>> 8);
target[idx + 7] = (byte) value;
}
/**
* Encodes a int value to the byte array.
*
* @param target The byte array to encode to.
* @param idx The starting index in the byte array.
* @param value The value to encode.
*/
public static void int4(byte[] target, int idx, int value) {
target[idx + 0] = (byte) (value >>> 24);
target[idx + 1] = (byte) (value >>> 16);
target[idx + 2] = (byte) (value >>> 8);
target[idx + 3] = (byte) value;
}
/**
* Encodes a int value to the byte array.
*
* @param target The byte array to encode to.
* @param idx The starting index in the byte array.
* @param value The value to encode.
*/
public static void int2(byte[] target, int idx, int value) {
target[idx + 0] = (byte) (value >>> 8);
target[idx + 1] = (byte) value;
}
/**
* Encodes a boolean value to the byte array.
*
* @param target
* The byte array to encode to.
* @param idx
* The starting index in the byte array.
* @param value
* The value to encode.
*/
public static void bool(byte[] target, int idx, boolean value) {
target[idx] = value ? (byte) 1 : (byte) 0;
}
/**
* Encodes a int value to the byte array.
*
* @param target The byte array to encode to.
* @param idx The starting index in the byte array.
* @param value The value to encode.
*/
public static void float4(byte[] target, int idx, float value) {
int4(target, idx, Float.floatToRawIntBits(value));
}
/**
* Encodes a int value to the byte array.
*
* @param target The byte array to encode to.
* @param idx The starting index in the byte array.
* @param value The value to encode.
*/
public static void float8(byte[] target, int idx, double value) {
int8(target, idx, Double.doubleToRawLongBits(value));
}
}