diff options
Diffstat (limited to 'javanano/src/main/java/com/google/protobuf/nano/InternalNano.java')
-rw-r--r-- | javanano/src/main/java/com/google/protobuf/nano/InternalNano.java | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java index 4a08bb58..67404d27 100644 --- a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java +++ b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java @@ -34,9 +34,10 @@ import com.google.protobuf.nano.MapFactories.MapFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.util.Arrays; -import java.util.Map; import java.util.Map.Entry; +import java.util.Map; /** * The classes contained within are used internally by the Protocol Buffer @@ -67,6 +68,8 @@ public final class InternalNano { public static final int TYPE_SINT32 = 17; public static final int TYPE_SINT64 = 18; + protected static final Charset UTF_8 = Charset.forName("UTF-8"); + protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); private InternalNano() {} @@ -111,14 +114,7 @@ public final class InternalNano { * generated code calls this automatically. */ public static String stringDefaultValue(String bytes) { - try { - return new String(bytes.getBytes("ISO-8859-1"), "UTF-8"); - } catch (UnsupportedEncodingException e) { - // This should never happen since all JVMs are required to implement - // both of the above character sets. - throw new IllegalStateException( - "Java VM does not support a standard character set.", e); - } + return new String(bytes.getBytes(ISO_8859_1), InternalNano.UTF_8); } /** @@ -130,14 +126,7 @@ public final class InternalNano { * embed raw bytes as a string literal with ISO-8859-1 encoding. */ public static byte[] bytesDefaultValue(String bytes) { - try { - return bytes.getBytes("ISO-8859-1"); - } catch (UnsupportedEncodingException e) { - // This should never happen since all JVMs are required to implement - // ISO-8859-1. - throw new IllegalStateException( - "Java VM does not support a standard character set.", e); - } + return bytes.getBytes(ISO_8859_1); } /** @@ -145,11 +134,7 @@ public final class InternalNano { * UnsupportedEncodingException to a RuntimeException. */ public static byte[] copyFromUtf8(final String text) { - try { - return text.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 not supported?"); - } + return text.getBytes(InternalNano.UTF_8); } /** |