/
ByteConverter.java
344 lines (308 loc) · 10.1 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (c) 2011, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.util;
import java.math.BigDecimal;
import java.nio.CharBuffer;
/**
* Helper methods to parse java base types from byte arrays.
*
* @author Mikko Tiihonen
*/
public class ByteConverter {
private static final int NBASE = 10000;
private static final int NUMERIC_DSCALE_MASK = 0x00003FFF;
private static final short NUMERIC_POS = 0x0000;
private static final short NUMERIC_NEG = 0x4000;
private static final short NUMERIC_NAN = (short) 0xC000;
private static final int DEC_DIGITS = 4;
private static final int[] round_powers = {0, 1000, 100, 10};
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");
}
}
/**
* Convert a number from binary representation to text representation.
* @param idx index of the digit to be converted in the digits array
* @param digits array of shorts that can be decoded as the number String
* @param buffer the character buffer to put the text representation in
* @param alwaysPutIt a flag that indicate whether or not to put the digit char even if it is zero
* @return String the number as String
*/
private static void digitToString(int idx, short[] digits, CharBuffer buffer, boolean alwaysPutIt) {
short dig = (idx >= 0 && idx < digits.length) ? digits[idx] : 0;
for (int p = 1; p < round_powers.length; p++) {
int pow = round_powers[p];
short d1 = (short)(dig / pow);
dig -= d1 * pow;
boolean putit = (d1 > 0);
if (putit || alwaysPutIt) {
buffer.put((char)(d1 + '0'));
}
}
buffer.put((char)(dig + '0'));
}
/**
* Convert a number from binary representation to text representation.
* @param digits array of shorts that can be decoded as the number String
* @param scale the scale of the number binary representation
* @param weight the weight of the number binary representation
* @param sign the sign of the number
* @return String the number as String
*/
private static String numberBytesToString(short[] digits, int scale, int weight, int sign) {
CharBuffer buffer;
int i;
int d;
/*
* Allocate space for the result.
*
* i is set to the # of decimal digits before decimal point. dscale is the
* # of decimal digits we will print after decimal point. We may generate
* as many as DEC_DIGITS-1 excess digits at the end, and in addition we
* need room for sign, decimal point, null terminator.
*/
i = (weight + 1) * DEC_DIGITS;
if (i <= 0) {
i = 1;
}
buffer = CharBuffer.allocate((i + scale + DEC_DIGITS + 2));
/*
* Output a dash for negative values
*/
if (sign == NUMERIC_NEG) {
buffer.put('-');
}
/*
* Output all digits before the decimal point
*/
if (weight < 0) {
d = weight + 1;
buffer.put('0');
} else {
for (d = 0; d <= weight; d++) {
/* In the first digit, suppress extra leading decimal zeroes */
digitToString(d, digits, buffer, d != 0);
}
}
/*
* If requested, output a decimal point and all the digits that follow it.
* We initially put out a multiple of DEC_DIGITS digits, then truncate if
* needed.
*/
if (scale > 0) {
buffer.put('.');
for (i = 0; i < scale; d++, i += DEC_DIGITS) {
digitToString(d, digits, buffer, true);
}
}
/*
* terminate the string and return it
*/
int extra = (i - scale) % DEC_DIGITS;
return new String(buffer.array(), 0, buffer.position() - extra);
}
/**
* 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 Number numeric(byte []bytes) {
if (bytes.length < 8) {
throw new IllegalArgumentException("number of bytes should be at-least 8");
}
short len = ByteConverter.int2(bytes, 0);
short weight = ByteConverter.int2(bytes, 2);
short sign = ByteConverter.int2(bytes, 4);
short scale = ByteConverter.int2(bytes, 6);
if (!(sign == NUMERIC_POS
|| sign == NUMERIC_NEG
|| sign == NUMERIC_NAN)) {
throw new IllegalArgumentException("invalid sign in \"numeric\" value");
}
if (sign == NUMERIC_NAN) {
return Double.NaN;
}
if ((scale & NUMERIC_DSCALE_MASK) != scale) {
throw new IllegalArgumentException("invalid scale in \"numeric\" value");
}
short[] digits = new short[len];
int idx = 8;
for (int i = 0; i < len; i++) {
short d = ByteConverter.int2(bytes, idx);
idx += 2;
if (d < 0 || d >= NBASE) {
throw new IllegalArgumentException("invalid digit in \"numeric\" value");
}
digits[i] = d;
}
String numString = numberBytesToString(digits, scale, weight, sign);
return new BigDecimal(numString);
}
/**
* 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));
}
}