|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.exporter.internal.marshal; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | + |
| 10 | +/** |
| 11 | + * Fallback StringEncoder implementation using standard Java string operations. |
| 12 | + * |
| 13 | + * <p>This implementation works on all Java versions and provides correct UTF-8 handling. |
| 14 | + * |
| 15 | + * <p>This class is internal and is hence not for public use. Its APIs are unstable and can change |
| 16 | + * at any time. |
| 17 | + */ |
| 18 | +final class FallbackStringEncoder implements StringEncoder { |
| 19 | + |
| 20 | + FallbackStringEncoder() {} |
| 21 | + |
| 22 | + @Override |
| 23 | + public int getUtf8Size(String string) { |
| 24 | + return encodedUtf8Length(string); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void writeUtf8(CodedOutputStream output, String string, int utf8Length) |
| 29 | + throws IOException { |
| 30 | + encodeUtf8(output, string); |
| 31 | + } |
| 32 | + |
| 33 | + // adapted from |
| 34 | + // https://github.com/protocolbuffers/protobuf/blob/b618f6750aed641a23d5f26fbbaf654668846d24/java/core/src/main/java/com/google/protobuf/Utf8.java#L217 |
| 35 | + private static int encodedUtf8Length(String string) { |
| 36 | + // Warning to maintainers: this implementation is highly optimized. |
| 37 | + int utf16Length = string.length(); |
| 38 | + int utf8Length = utf16Length; |
| 39 | + int i = 0; |
| 40 | + |
| 41 | + // This loop optimizes for pure ASCII. |
| 42 | + while (i < utf16Length && string.charAt(i) < 0x80) { |
| 43 | + i++; |
| 44 | + } |
| 45 | + |
| 46 | + // This loop optimizes for chars less than 0x800. |
| 47 | + for (; i < utf16Length; i++) { |
| 48 | + char c = string.charAt(i); |
| 49 | + if (c < 0x800) { |
| 50 | + utf8Length += ((0x7f - c) >>> 31); // branch free! |
| 51 | + } else { |
| 52 | + utf8Length += encodedUtf8LengthGeneral(string, i); |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (utf8Length < utf16Length) { |
| 58 | + // Necessary and sufficient condition for overflow because of maximum 3x expansion |
| 59 | + throw new IllegalArgumentException( |
| 60 | + "UTF-8 length does not fit in int: " + (utf8Length + (1L << 32))); |
| 61 | + } |
| 62 | + |
| 63 | + return utf8Length; |
| 64 | + } |
| 65 | + |
| 66 | + // adapted from |
| 67 | + // https://github.com/protocolbuffers/protobuf/blob/b618f6750aed641a23d5f26fbbaf654668846d24/java/core/src/main/java/com/google/protobuf/Utf8.java#L247 |
| 68 | + private static int encodedUtf8LengthGeneral(String string, int start) { |
| 69 | + int utf16Length = string.length(); |
| 70 | + int utf8Length = 0; |
| 71 | + for (int i = start; i < utf16Length; i++) { |
| 72 | + char c = string.charAt(i); |
| 73 | + if (c < 0x800) { |
| 74 | + utf8Length += (0x7f - c) >>> 31; // branch free! |
| 75 | + } else { |
| 76 | + utf8Length += 2; |
| 77 | + if (Character.isSurrogate(c)) { |
| 78 | + // Check that we have a well-formed surrogate pair. |
| 79 | + if (Character.codePointAt(string, i) != c) { |
| 80 | + i++; |
| 81 | + } else { |
| 82 | + // invalid sequence |
| 83 | + // At this point we have accumulated 3 byes of length (2 in this method and 1 in caller) |
| 84 | + // for current character, reduce the length to 1 bytes as we are going to encode the |
| 85 | + // invalid character as ? |
| 86 | + utf8Length -= 2; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return utf8Length; |
| 93 | + } |
| 94 | + |
| 95 | + // encode utf8 the same way as length is computed in encodedUtf8Length |
| 96 | + // adapted from |
| 97 | + // https://github.com/protocolbuffers/protobuf/blob/b618f6750aed641a23d5f26fbbaf654668846d24/java/core/src/main/java/com/google/protobuf/Utf8.java#L1016 |
| 98 | + private static void encodeUtf8(CodedOutputStream output, String in) throws IOException { |
| 99 | + int utf16Length = in.length(); |
| 100 | + int i = 0; |
| 101 | + // Designed to take advantage of |
| 102 | + // https://wiki.openjdk.java.net/display/HotSpotInternals/RangeCheckElimination |
| 103 | + for (char c; i < utf16Length && (c = in.charAt(i)) < 0x80; i++) { |
| 104 | + output.write((byte) c); |
| 105 | + } |
| 106 | + if (i == utf16Length) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + for (char c; i < utf16Length; i++) { |
| 111 | + c = in.charAt(i); |
| 112 | + if (c < 0x80) { |
| 113 | + // 1 byte, 7 bits |
| 114 | + output.write((byte) c); |
| 115 | + } else if (c < 0x800) { // 11 bits, two UTF-8 bytes |
| 116 | + output.write((byte) ((0xF << 6) | (c >>> 6))); |
| 117 | + output.write((byte) (0x80 | (0x3F & c))); |
| 118 | + } else if (!Character.isSurrogate(c)) { |
| 119 | + // Maximum single-char code point is 0xFFFF, 16 bits, three UTF-8 bytes |
| 120 | + output.write((byte) ((0xF << 5) | (c >>> 12))); |
| 121 | + output.write((byte) (0x80 | (0x3F & (c >>> 6)))); |
| 122 | + output.write((byte) (0x80 | (0x3F & c))); |
| 123 | + } else { |
| 124 | + // Minimum code point represented by a surrogate pair is 0x10000, 17 bits, |
| 125 | + // four UTF-8 bytes |
| 126 | + int codePoint = Character.codePointAt(in, i); |
| 127 | + if (codePoint != c) { |
| 128 | + output.write((byte) ((0xF << 4) | (codePoint >>> 18))); |
| 129 | + output.write((byte) (0x80 | (0x3F & (codePoint >>> 12)))); |
| 130 | + output.write((byte) (0x80 | (0x3F & (codePoint >>> 6)))); |
| 131 | + output.write((byte) (0x80 | (0x3F & codePoint))); |
| 132 | + i++; |
| 133 | + } else { |
| 134 | + // invalid sequence |
| 135 | + output.write((byte) '?'); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments