diff options
Diffstat (limited to 'java/src/main/java/com/google/protobuf/LiteralByteString.java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/LiteralByteString.java | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/java/src/main/java/com/google/protobuf/LiteralByteString.java b/java/src/main/java/com/google/protobuf/LiteralByteString.java index 767b9f35..6893ddf1 100644 --- a/java/src/main/java/com/google/protobuf/LiteralByteString.java +++ b/java/src/main/java/com/google/protobuf/LiteralByteString.java @@ -154,7 +154,11 @@ class LiteralByteString extends ByteString { @Override public String toString(String charsetName) throws UnsupportedEncodingException { - return new String(bytes, getOffsetIntoBytes(), size(), charsetName); + // Optimize for empty strings, but ensure we don't silently ignore invalid + // encodings. + return size() == 0 && UTF_8.equals(charsetName) + ? "" + : new String(bytes, getOffsetIntoBytes(), size(), charsetName); } // ================================================================= |