From 4ddf824f81d1e4b5ebdfd988ccc140686b9fa3f5 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Wed, 12 Sep 2018 18:01:17 -0700 Subject: Make LowerCaseBase16Encoding package protected. (#1427) --- .../opencensus/trace/LowerCaseBase16Encoding.java | 91 +++++++++++++++++++++ api/src/main/java/io/opencensus/trace/SpanId.java | 1 - api/src/main/java/io/opencensus/trace/TraceId.java | 1 - .../trace/internal/LowerCaseBase16Encoding.java | 93 ---------------------- 4 files changed, 91 insertions(+), 95 deletions(-) create mode 100644 api/src/main/java/io/opencensus/trace/LowerCaseBase16Encoding.java delete mode 100644 api/src/main/java/io/opencensus/trace/internal/LowerCaseBase16Encoding.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/io/opencensus/trace/LowerCaseBase16Encoding.java b/api/src/main/java/io/opencensus/trace/LowerCaseBase16Encoding.java new file mode 100644 index 00000000..bca95868 --- /dev/null +++ b/api/src/main/java/io/opencensus/trace/LowerCaseBase16Encoding.java @@ -0,0 +1,91 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.trace; + +import io.opencensus.internal.Utils; +import java.util.Arrays; + +/** Internal copy of the Guava implementation of the {@code BaseEncoding.base16().lowerCase()}. */ +final class LowerCaseBase16Encoding { + private static final String ALPHABET = "0123456789abcdef"; + private static final int ASCII_CHARACTERS = 128; + private static final char[] ENCODING = buildEncodingArray(); + private static final byte[] DECODING = buildDecodingArray(); + + 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[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; + } + + /** + * Encodes the specified byte array, and returns the encoded {@code String}. + * + * @param bytes byte array to be encoded. + * @return the encoded {@code String}. + */ + static String encodeToString(byte[] bytes) { + StringBuilder stringBuilder = new StringBuilder(bytes.length * 2); + for (byte byteVal : bytes) { + int b = byteVal & 0xFF; + stringBuilder.append(ENCODING[b]); + stringBuilder.append(ENCODING[b | 0x100]); + } + return stringBuilder.toString(); + } + + /** + * Decodes the specified character sequence, and returns the resulting {@code byte[]}. + * + * @param chars the character sequence to be decoded. + * @return the resulting {@code byte[]} + * @throws IllegalArgumentException if the input is not a valid encoded string according to this + * encoding. + */ + static byte[] decodeToBytes(CharSequence chars) { + Utils.checkArgument(chars.length() % 2 == 0, "Invalid input length " + chars.length()); + int bytesWritten = 0; + byte[] bytes = new byte[chars.length() / 2]; + for (int i = 0; i < chars.length(); i += 2) { + bytes[bytesWritten++] = decodeByte(chars.charAt(i), chars.charAt(i + 1)); + } + return bytes; + } + + private static byte decodeByte(char hi, char lo) { + Utils.checkArgument(lo < ASCII_CHARACTERS && DECODING[lo] != -1, "Invalid character " + lo); + Utils.checkArgument(hi < ASCII_CHARACTERS && DECODING[hi] != -1, "Invalid character " + hi); + int decoded = DECODING[hi] << 4 | DECODING[lo]; + return (byte) decoded; + } + + // Private constructor to disallow instances. + private LowerCaseBase16Encoding() {} +} diff --git a/api/src/main/java/io/opencensus/trace/SpanId.java b/api/src/main/java/io/opencensus/trace/SpanId.java index 44119d6b..c43fa6b0 100644 --- a/api/src/main/java/io/opencensus/trace/SpanId.java +++ b/api/src/main/java/io/opencensus/trace/SpanId.java @@ -17,7 +17,6 @@ package io.opencensus.trace; import io.opencensus.internal.Utils; -import io.opencensus.trace.internal.LowerCaseBase16Encoding; import java.util.Arrays; import java.util.Random; import javax.annotation.Nullable; diff --git a/api/src/main/java/io/opencensus/trace/TraceId.java b/api/src/main/java/io/opencensus/trace/TraceId.java index 983b21c3..465e4d4a 100644 --- a/api/src/main/java/io/opencensus/trace/TraceId.java +++ b/api/src/main/java/io/opencensus/trace/TraceId.java @@ -18,7 +18,6 @@ package io.opencensus.trace; import io.opencensus.common.Internal; import io.opencensus.internal.Utils; -import io.opencensus.trace.internal.LowerCaseBase16Encoding; import java.util.Arrays; import java.util.Random; import javax.annotation.Nullable; diff --git a/api/src/main/java/io/opencensus/trace/internal/LowerCaseBase16Encoding.java b/api/src/main/java/io/opencensus/trace/internal/LowerCaseBase16Encoding.java deleted file mode 100644 index d9b2cf09..00000000 --- a/api/src/main/java/io/opencensus/trace/internal/LowerCaseBase16Encoding.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2018, OpenCensus Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opencensus.trace.internal; - -import io.opencensus.common.Internal; -import io.opencensus.internal.Utils; -import java.util.Arrays; - -/** Internal copy of the Guava implementation of the {@code BaseEncoding.base16().lowerCase()}. */ -@Internal -public final class LowerCaseBase16Encoding { - private static final String ALPHABET = "0123456789abcdef"; - private static final int ASCII_CHARACTERS = 128; - private static final char[] ENCODING = buildEncodingArray(); - private static final byte[] DECODING = buildDecodingArray(); - - 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[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; - } - - /** - * Encodes the specified byte array, and returns the encoded {@code String}. - * - * @param bytes byte array to be encoded. - * @return the encoded {@code String}. - */ - public static String encodeToString(byte[] bytes) { - StringBuilder stringBuilder = new StringBuilder(bytes.length * 2); - for (byte byteVal : bytes) { - int b = byteVal & 0xFF; - stringBuilder.append(ENCODING[b]); - stringBuilder.append(ENCODING[b | 0x100]); - } - return stringBuilder.toString(); - } - - /** - * Decodes the specified character sequence, and returns the resulting {@code byte[]}. - * - * @param chars the character sequence to be decoded. - * @return the resulting {@code byte[]} - * @throws IllegalArgumentException if the input is not a valid encoded string according to this - * encoding. - */ - public static byte[] decodeToBytes(CharSequence chars) { - Utils.checkArgument(chars.length() % 2 == 0, "Invalid input length " + chars.length()); - int bytesWritten = 0; - byte[] bytes = new byte[chars.length() / 2]; - for (int i = 0; i < chars.length(); i += 2) { - bytes[bytesWritten++] = decodeByte(chars.charAt(i), chars.charAt(i + 1)); - } - return bytes; - } - - private static byte decodeByte(char hi, char lo) { - Utils.checkArgument(lo < ASCII_CHARACTERS && DECODING[lo] != -1, "Invalid character " + lo); - Utils.checkArgument(hi < ASCII_CHARACTERS && DECODING[hi] != -1, "Invalid character " + hi); - int decoded = DECODING[hi] << 4 | DECODING[lo]; - return (byte) decoded; - } - - // Private constructor to disallow instances. - private LowerCaseBase16Encoding() {} -} -- cgit v1.2.3