diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-10-05 15:04:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-05 15:04:38 +0200 |
commit | 3f96fe4e1a96b3b21c913a392da87e5ec669b286 (patch) | |
tree | bfcfbad0db1a54372fec7e483749706bdc55f6f6 /vespajlib/src/main/java/com/yahoo/text | |
parent | 233b521555e240458e38f555430459c3e30f0d64 (diff) |
Revert "Remove unused Utf8 methods"
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/text')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/text/StringUtilities.java | 14 | ||||
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/text/Utf8.java | 14 |
2 files changed, 21 insertions, 7 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java b/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java index 74458be4584..370d079b3ec 100644 --- a/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java +++ b/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java @@ -1,9 +1,9 @@ -// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.text; import com.google.common.collect.ImmutableSet; -import java.nio.charset.StandardCharsets; +import java.nio.charset.Charset; import java.util.Collections; import java.util.List; import java.io.ByteArrayOutputStream; @@ -19,6 +19,8 @@ import java.util.Set; // TODO: Text utilities should which are still needed should move to Text. This should be deprecated. public class StringUtilities { + private static Charset UTF8 = Charset.forName("utf8"); + private static byte toHex(int val) { return (byte) (val < 10 ? '0' + val : 'a' + (val - 10)); } private static class ReplacementCharacters { @@ -63,7 +65,7 @@ public class StringUtilities { * @return The escaped string */ public static String escape(String source, char delimiter) { - byte bytes[] = source.getBytes(StandardCharsets.UTF_8); + byte bytes[] = source.getBytes(UTF8); ByteArrayOutputStream result = new ByteArrayOutputStream(); for (byte b : bytes) { int val = b; @@ -84,11 +86,11 @@ public class StringUtilities { result.write(replacementCharacters.replacement2[val]); } } - return new String(result.toByteArray(), StandardCharsets.UTF_8); + return new String(result.toByteArray(), UTF8); } public static String unescape(String source) { - byte bytes[] = source.getBytes(StandardCharsets.UTF_8); + byte bytes[] = source.getBytes(UTF8); ByteArrayOutputStream result = new ByteArrayOutputStream(); for (int i=0; i<bytes.length; ++i) { if (bytes[i] != '\\') { @@ -118,7 +120,7 @@ public class StringUtilities { result.write((byte) Integer.parseInt(hexdigits, 16)); i += 3; } - return new String(result.toByteArray(), StandardCharsets.UTF_8); + return new String(result.toByteArray(), UTF8); } /** diff --git a/vespajlib/src/main/java/com/yahoo/text/Utf8.java b/vespajlib/src/main/java/com/yahoo/text/Utf8.java index e882353469e..b81f0447d04 100644 --- a/vespajlib/src/main/java/com/yahoo/text/Utf8.java +++ b/vespajlib/src/main/java/com/yahoo/text/Utf8.java @@ -1,4 +1,4 @@ -// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.text; import java.io.IOException; @@ -31,6 +31,11 @@ public final class Utf8 { return StandardCharsets.UTF_8; } + /** To be used instead of String.String(byte[] bytes) */ + public static String toStringStd(byte[] data) { + return new String(data, StandardCharsets.UTF_8); + } + /** * Utility method as toString(byte[]). * @@ -61,6 +66,13 @@ public final class Utf8 { } /** + * Uses String.getBytes directly. + */ + public static byte[] toBytesStd(String str) { + return str.getBytes(StandardCharsets.UTF_8); + } + + /** * Encode a long as its decimal representation, i.e. toAsciiBytes(15L) will * return "15" encoded as UTF-8. In other words it is an optimized version * of String.valueOf() followed by UTF-8 encoding. Avoid going through |