Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.exporter.internal.marshal;

import java.io.IOException;
import javax.annotation.Nullable;

/**
* This class contains shared logic for UTF-8 encoding operations while allowing subclasses to
Expand All @@ -20,8 +21,11 @@ abstract class AbstractStringEncoder implements StringEncoder {
private final FallbackStringEncoder fallback = new FallbackStringEncoder();

@Override
public final void writeUtf8(CodedOutputStream output, String string, int utf8Length)
public final void writeUtf8(CodedOutputStream output, @Nullable String string, int utf8Length)
throws IOException {
if (string == null) {
return;
}
// if the length of the latin1 string and the utf8 output are the same then the string must be
// composed of only 7bit characters and can be directly copied to the output
if (string.length() == utf8Length && isLatin1(string)) {
Expand All @@ -33,8 +37,10 @@ public final void writeUtf8(CodedOutputStream output, String string, int utf8Len
}

@Override
public final int getUtf8Size(String string) {
if (isLatin1(string)) {
public final int getUtf8Size(@Nullable String string) {
if (string == null) {
return 0;
} else if (isLatin1(string)) {
byte[] bytes = getStringBytes(string);
// latin1 bytes with negative value (most significant bit set) are encoded as 2 bytes in utf8
return string.length() + countNegative(bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.exporter.internal.marshal;

import java.io.IOException;
import javax.annotation.Nullable;

/**
* Fallback StringEncoder implementation using standard Java string operations.
Expand All @@ -20,14 +21,16 @@ final class FallbackStringEncoder implements StringEncoder {
FallbackStringEncoder() {}

@Override
public int getUtf8Size(String string) {
return encodedUtf8Length(string);
public int getUtf8Size(@Nullable String string) {
return string != null ? encodedUtf8Length(string) : 0;
}

@Override
public void writeUtf8(CodedOutputStream output, String string, int utf8Length)
public void writeUtf8(CodedOutputStream output, @Nullable String string, int utf8Length)
throws IOException {
encodeUtf8(output, string);
if (string != null) {
encodeUtf8(output, string);
}
}

// adapted from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.exporter.internal.marshal;

import java.io.IOException;
import javax.annotation.Nullable;

/**
* Interface for efficient UTF-8 string encoding operations.
Expand All @@ -29,13 +30,14 @@
public interface StringEncoder {

/** Returns the number of bytes required to encode the string as UTF-8. */
int getUtf8Size(String string);
int getUtf8Size(@Nullable String string);

/**
* Write a string as UTF-8 bytes to the output stream using the pre-calculated UTF-8 length from
* {@link #getUtf8Size(String)}.
*/
void writeUtf8(CodedOutputStream output, String string, int utf8Length) throws IOException;
void writeUtf8(CodedOutputStream output, @Nullable String string, int utf8Length)
throws IOException;

/** Returns the best available StringEncoder implementation. */
static StringEncoder getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ void testUtf8SizeLatin1_VarHandle() {
private static void testUtf8Encoding(StringEncoder stringEncoder) {
assertThat(stringEncoder).isNotNull();

assertThat(stringEncoder.getUtf8Size(null)).isEqualTo(0);
assertThat(testUtf8(null, 0, stringEncoder)).isEqualTo("");

assertThat(stringEncoder.getUtf8Size("")).isEqualTo(0);
assertThat(testUtf8("", 0, stringEncoder)).isEqualTo("");

Expand All @@ -87,6 +90,8 @@ private static void testUtf8Encoding(StringEncoder stringEncoder) {
}

private static void testUtf8SizeLatin1(StringEncoder stringEncoder) {
assertThat(stringEncoder.getUtf8Size(null)).isEqualTo(0);

// Run repeated test logic for each encoder
Random random = new Random();
for (int i = 0; i < 1000; i++) {
Expand Down
Loading