forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOtelEncodingUtils.java
More file actions
162 lines (146 loc) · 6.23 KB
/
OtelEncodingUtils.java
File metadata and controls
162 lines (146 loc) · 6.23 KB
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.api.internal;
import java.util.Arrays;
import javax.annotation.concurrent.Immutable;
/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
@Immutable
public final class OtelEncodingUtils {
static final int LONG_BYTES = Long.SIZE / Byte.SIZE;
static final int BYTE_BASE16 = 2;
static final int LONG_BASE16 = BYTE_BASE16 * LONG_BYTES;
private static final String ALPHABET = "0123456789abcdef";
private static final int NUM_ASCII_CHARACTERS = 128;
private static final char[] ENCODING = buildEncodingArray();
private static final byte[] DECODING = buildDecodingArray();
private static final boolean[] VALID_HEX = buildValidHexArray();
private static char[] buildEncodingArray() {
char[] encoding = new char[512];
for (int i = 0; i < 256; ++i) {
encoding[i] = ALPHABET.charAt(i >>> 4);
encoding[i | 0x100] = ALPHABET.charAt(i & 0xF);
}
return encoding;
}
private static byte[] buildDecodingArray() {
byte[] decoding = new byte[NUM_ASCII_CHARACTERS];
Arrays.fill(decoding, (byte) -1);
for (int i = 0; i < ALPHABET.length(); i++) {
char c = ALPHABET.charAt(i);
decoding[c] = (byte) i;
}
return decoding;
}
private static boolean[] buildValidHexArray() {
boolean[] validHex = new boolean[Character.MAX_VALUE];
// Use the fact that the default initial boolean value is false
// and only explicitly init the values between the extreme HEX values
// This minimized impact during process startup esp. on mobile.
for (int i = 48; i < 103; i++) {
validHex[i] = i <= 57 || 97 <= i;
}
return validHex;
}
/**
* Returns the {@code long} value whose base16 representation is stored in the first 16 chars of
* {@code chars} starting from the {@code offset}.
*
* @param chars the base16 representation of the {@code long}.
* @param offset the starting offset in the {@code CharSequence}.
*/
public static long longFromBase16String(CharSequence chars, int offset) {
return (byteFromBase16(chars.charAt(offset), chars.charAt(offset + 1)) & 0xFFL) << 56
| (byteFromBase16(chars.charAt(offset + 2), chars.charAt(offset + 3)) & 0xFFL) << 48
| (byteFromBase16(chars.charAt(offset + 4), chars.charAt(offset + 5)) & 0xFFL) << 40
| (byteFromBase16(chars.charAt(offset + 6), chars.charAt(offset + 7)) & 0xFFL) << 32
| (byteFromBase16(chars.charAt(offset + 8), chars.charAt(offset + 9)) & 0xFFL) << 24
| (byteFromBase16(chars.charAt(offset + 10), chars.charAt(offset + 11)) & 0xFFL) << 16
| (byteFromBase16(chars.charAt(offset + 12), chars.charAt(offset + 13)) & 0xFFL) << 8
| (byteFromBase16(chars.charAt(offset + 14), chars.charAt(offset + 15)) & 0xFFL);
}
/**
* Appends the base16 encoding of the specified {@code value} to the {@code dest}.
*
* @param value the value to be converted.
* @param dest the destination char array.
* @param destOffset the starting offset in the destination char array.
*/
public static void longToBase16String(long value, char[] dest, int destOffset) {
byteToBase16((byte) (value >> 56 & 0xFFL), dest, destOffset);
byteToBase16((byte) (value >> 48 & 0xFFL), dest, destOffset + BYTE_BASE16);
byteToBase16((byte) (value >> 40 & 0xFFL), dest, destOffset + 2 * BYTE_BASE16);
byteToBase16((byte) (value >> 32 & 0xFFL), dest, destOffset + 3 * BYTE_BASE16);
byteToBase16((byte) (value >> 24 & 0xFFL), dest, destOffset + 4 * BYTE_BASE16);
byteToBase16((byte) (value >> 16 & 0xFFL), dest, destOffset + 5 * BYTE_BASE16);
byteToBase16((byte) (value >> 8 & 0xFFL), dest, destOffset + 6 * BYTE_BASE16);
byteToBase16((byte) (value & 0xFFL), dest, destOffset + 7 * BYTE_BASE16);
}
/** Returns the {@code byte[]} decoded from the given hex {@link CharSequence}. */
public static byte[] bytesFromBase16(CharSequence value, int length) {
byte[] result = new byte[length / 2];
bytesFromBase16(value, length, result);
return result;
}
/** Fills {@code bytes} with bytes decoded from the given hex {@link CharSequence}. */
public static void bytesFromBase16(CharSequence value, int length, byte[] bytes) {
for (int i = 0; i < length; i += 2) {
bytes[i / 2] = byteFromBase16(value.charAt(i), value.charAt(i + 1));
}
}
/** Fills {@code dest} with the hex encoding of {@code bytes}. */
public static void bytesToBase16(byte[] bytes, char[] dest, int length) {
for (int i = 0; i < length; i++) {
byteToBase16(bytes[i], dest, i * 2);
}
}
/**
* Encodes the specified byte, and returns the encoded {@code String}.
*
* @param value the value to be converted.
* @param dest the destination char array.
* @param destOffset the starting offset in the destination char array.
*/
public static void byteToBase16(byte value, char[] dest, int destOffset) {
int b = value & 0xFF;
dest[destOffset] = ENCODING[b];
dest[destOffset + 1] = ENCODING[b | 0x100];
}
/**
* Decodes the specified two character sequence, and returns the resulting {@code byte}.
*
* @param first the first hex character.
* @param second the second hex character.
* @return the resulting {@code byte}
*/
public static byte byteFromBase16(char first, char second) {
if (first >= NUM_ASCII_CHARACTERS || DECODING[first] == -1) {
throw new IllegalArgumentException("invalid character " + first);
}
if (second >= NUM_ASCII_CHARACTERS || DECODING[second] == -1) {
throw new IllegalArgumentException("invalid character " + second);
}
int decoded = DECODING[first] << 4 | DECODING[second];
return (byte) decoded;
}
/** Returns whether the {@link CharSequence} is a valid hex string. */
public static boolean isValidBase16String(CharSequence value) {
int len = value.length();
for (int i = 0; i < len; i++) {
char b = value.charAt(i);
if (!isValidBase16Character(b)) {
return false;
}
}
return true;
}
/** Returns whether the given {@code char} is a valid hex character. */
public static boolean isValidBase16Character(char b) {
return VALID_HEX[b];
}
private OtelEncodingUtils() {}
}