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 ---------------------- .../trace/LowerCaseBase16EncodingTest.java | 83 +++++++++++++++++++ .../internal/LowerCaseBase16EncodingTest.java | 83 ------------------- 6 files changed, 174 insertions(+), 178 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 create mode 100644 api/src/test/java/io/opencensus/trace/LowerCaseBase16EncodingTest.java delete mode 100644 api/src/test/java/io/opencensus/trace/internal/LowerCaseBase16EncodingTest.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() {} -} diff --git a/api/src/test/java/io/opencensus/trace/LowerCaseBase16EncodingTest.java b/api/src/test/java/io/opencensus/trace/LowerCaseBase16EncodingTest.java new file mode 100644 index 00000000..3444d2b3 --- /dev/null +++ b/api/src/test/java/io/opencensus/trace/LowerCaseBase16EncodingTest.java @@ -0,0 +1,83 @@ +/* + * 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 static com.google.common.truth.Truth.assertThat; + +import java.nio.charset.Charset; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link io.opencensus.trace.LowerCaseBase16Encoding}. */ +@RunWith(JUnit4.class) +public class LowerCaseBase16EncodingTest { + private static final Charset CHARSET = Charset.forName("UTF-8"); + + @Rule public ExpectedException thrown = ExpectedException.none(); + + @Test + public void valid_EncodeDecode() { + testEncoding("", ""); + testEncoding("f", "66"); + testEncoding("fo", "666f"); + testEncoding("foo", "666f6f"); + testEncoding("foob", "666f6f62"); + testEncoding("fooba", "666f6f6261"); + testEncoding("foobar", "666f6f626172"); + } + + @Test + public void invalidDecodings_UnrecongnizedCharacters() { + // These contain bytes not in the decoding. + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Invalid character g"); + LowerCaseBase16Encoding.decodeToBytes("efhg"); + } + + @Test + public void invalidDecodings_InvalidInputLength() { + // Valid base16 strings always have an even length. + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Invalid input length 3"); + LowerCaseBase16Encoding.decodeToBytes("abc"); + } + + @Test + public void invalidDecodings_InvalidInputLengthAndCharacter() { + // These have a combination of invalid length and unrecognized characters. + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Invalid input length 1"); + LowerCaseBase16Encoding.decodeToBytes("?"); + } + + private static void testEncoding(String decoded, String encoded) { + testEncodes(decoded, encoded); + testDecodes(encoded, decoded); + } + + private static void testEncodes(String decoded, String encoded) { + assertThat(LowerCaseBase16Encoding.encodeToString(decoded.getBytes(CHARSET))) + .isEqualTo(encoded); + } + + private static void testDecodes(String encoded, String decoded) { + assertThat(LowerCaseBase16Encoding.decodeToBytes(encoded)).isEqualTo(decoded.getBytes(CHARSET)); + } +} diff --git a/api/src/test/java/io/opencensus/trace/internal/LowerCaseBase16EncodingTest.java b/api/src/test/java/io/opencensus/trace/internal/LowerCaseBase16EncodingTest.java deleted file mode 100644 index bcc51ab6..00000000 --- a/api/src/test/java/io/opencensus/trace/internal/LowerCaseBase16EncodingTest.java +++ /dev/null @@ -1,83 +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 static com.google.common.truth.Truth.assertThat; - -import java.nio.charset.Charset; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Unit tests for {@link LowerCaseBase16Encoding}. */ -@RunWith(JUnit4.class) -public class LowerCaseBase16EncodingTest { - private static final Charset CHARSET = Charset.forName("UTF-8"); - - @Rule public ExpectedException thrown = ExpectedException.none(); - - @Test - public void valid_EncodeDecode() { - testEncoding("", ""); - testEncoding("f", "66"); - testEncoding("fo", "666f"); - testEncoding("foo", "666f6f"); - testEncoding("foob", "666f6f62"); - testEncoding("fooba", "666f6f6261"); - testEncoding("foobar", "666f6f626172"); - } - - @Test - public void invalidDecodings_UnrecongnizedCharacters() { - // These contain bytes not in the decoding. - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Invalid character g"); - LowerCaseBase16Encoding.decodeToBytes("efhg"); - } - - @Test - public void invalidDecodings_InvalidInputLength() { - // Valid base16 strings always have an even length. - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Invalid input length 3"); - LowerCaseBase16Encoding.decodeToBytes("abc"); - } - - @Test - public void invalidDecodings_InvalidInputLengthAndCharacter() { - // These have a combination of invalid length and unrecognized characters. - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Invalid input length 1"); - LowerCaseBase16Encoding.decodeToBytes("?"); - } - - private static void testEncoding(String decoded, String encoded) { - testEncodes(decoded, encoded); - testDecodes(encoded, decoded); - } - - private static void testEncodes(String decoded, String encoded) { - assertThat(LowerCaseBase16Encoding.encodeToString(decoded.getBytes(CHARSET))) - .isEqualTo(encoded); - } - - private static void testDecodes(String encoded, String decoded) { - assertThat(LowerCaseBase16Encoding.decodeToBytes(encoded)).isEqualTo(decoded.getBytes(CHARSET)); - } -} -- cgit v1.2.3