aboutsummaryrefslogtreecommitdiff
path: root/java/src/main
diff options
context:
space:
mode:
authorTamir Duberstein <tamird@gmail.com>2015-03-31 20:01:43 -0700
committerTamir Duberstein <tamird@gmail.com>2015-04-02 15:06:00 -0700
commit210de285d7ca5e4d4e96867c1c04308d68c5d4dd (patch)
tree983ab06e7e3a37e38a5d6c1768c15f50246df5d3 /java/src/main
parente84893f6768f136cc86e2db69fc1d40ff2be7e3b (diff)
downloadprotobuf-210de285d7ca5e4d4e96867c1c04308d68c5d4dd.tar.gz
protobuf-210de285d7ca5e4d4e96867c1c04308d68c5d4dd.tar.bz2
protobuf-210de285d7ca5e4d4e96867c1c04308d68c5d4dd.zip
DRY: Use `Charset` statics to eliminate exceptions
Diffstat (limited to 'java/src/main')
-rw-r--r--java/src/main/java/com/google/protobuf/ByteString.java12
-rw-r--r--java/src/main/java/com/google/protobuf/CodedInputStream.java6
-rw-r--r--java/src/main/java/com/google/protobuf/CodedOutputStream.java12
-rw-r--r--java/src/main/java/com/google/protobuf/Descriptors.java7
-rw-r--r--java/src/main/java/com/google/protobuf/Internal.java45
-rw-r--r--java/src/main/java/com/google/protobuf/Utf8.java2
6 files changed, 31 insertions, 53 deletions
diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java
index ee2eddb0..04ac7c9a 100644
--- a/java/src/main/java/com/google/protobuf/ByteString.java
+++ b/java/src/main/java/com/google/protobuf/ByteString.java
@@ -263,6 +263,18 @@ public abstract class ByteString implements Iterable<Byte>, Serializable {
}
/**
+ * Encodes {@code text} into a sequence of bytes using the named charset
+ * and returns the result as a {@code ByteString}.
+ *
+ * @param text source string
+ * @param charset encode using this charset
+ * @return new {@code ByteString}
+ */
+ public static ByteString copyFrom(String text, Charset charset) {
+ return new LiteralByteString(text.getBytes(charset));
+ }
+
+ /**
* Encodes {@code text} into a sequence of UTF-8 bytes and returns the
* result as a {@code ByteString}.
*
diff --git a/java/src/main/java/com/google/protobuf/CodedInputStream.java b/java/src/main/java/com/google/protobuf/CodedInputStream.java
index 0ca00bab..b15c2731 100644
--- a/java/src/main/java/com/google/protobuf/CodedInputStream.java
+++ b/java/src/main/java/com/google/protobuf/CodedInputStream.java
@@ -373,14 +373,14 @@ public final class CodedInputStream {
if (size <= (bufferSize - bufferPos) && size > 0) {
// Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it.
- final String result = new String(buffer, bufferPos, size, "UTF-8");
+ final String result = new String(buffer, bufferPos, size, ByteString.UTF_8);
bufferPos += size;
return result;
} else if (size == 0) {
return "";
} else {
// Slow path: Build a byte array first then copy it.
- return new String(readRawBytesSlowPath(size), "UTF-8");
+ return new String(readRawBytesSlowPath(size), ByteString.UTF_8);
}
}
@@ -409,7 +409,7 @@ public final class CodedInputStream {
if (!Utf8.isValidUtf8(bytes, pos, pos + size)) {
throw InvalidProtocolBufferException.invalidUtf8();
}
- return new String(bytes, pos, size, "UTF-8");
+ return new String(bytes, pos, size, ByteString.UTF_8);
}
/** Read a {@code group} field value from the stream. */
diff --git a/java/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/src/main/java/com/google/protobuf/CodedOutputStream.java
index cb318480..9994fd3e 100644
--- a/java/src/main/java/com/google/protobuf/CodedOutputStream.java
+++ b/java/src/main/java/com/google/protobuf/CodedOutputStream.java
@@ -420,7 +420,7 @@ public final class CodedOutputStream {
// Unfortunately there does not appear to be any way to tell Java to encode
// UTF-8 directly into our buffer, so we have to let it create its own byte
// array and then copy.
- final byte[] bytes = value.getBytes("UTF-8");
+ final byte[] bytes = value.getBytes(ByteString.UTF_8);
writeRawVarint32(bytes.length);
writeRawBytes(bytes);
}
@@ -827,13 +827,9 @@ public final class CodedOutputStream {
* {@code string} field.
*/
public static int computeStringSizeNoTag(final String value) {
- try {
- final byte[] bytes = value.getBytes("UTF-8");
- return computeRawVarint32Size(bytes.length) +
- bytes.length;
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("UTF-8 not supported.", e);
- }
+ final byte[] bytes = value.getBytes(ByteString.UTF_8);
+ return computeRawVarint32Size(bytes.length) +
+ bytes.length;
}
/**
diff --git a/java/src/main/java/com/google/protobuf/Descriptors.java b/java/src/main/java/com/google/protobuf/Descriptors.java
index d819a8ad..ab8930a1 100644
--- a/java/src/main/java/com/google/protobuf/Descriptors.java
+++ b/java/src/main/java/com/google/protobuf/Descriptors.java
@@ -319,12 +319,7 @@ public final class Descriptors {
}
final byte[] descriptorBytes;
- try {
- descriptorBytes = descriptorData.toString().getBytes("ISO-8859-1");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(
- "Standard encoding ISO-8859-1 not supported by JVM.", e);
- }
+ descriptorBytes = descriptorData.toString().getBytes(Internal.ISO_8859_1);
FileDescriptorProto proto;
try {
diff --git a/java/src/main/java/com/google/protobuf/Internal.java b/java/src/main/java/com/google/protobuf/Internal.java
index b9a8a994..8a8bcda1 100644
--- a/java/src/main/java/com/google/protobuf/Internal.java
+++ b/java/src/main/java/com/google/protobuf/Internal.java
@@ -33,6 +33,7 @@ package com.google.protobuf;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractSet;
@@ -51,6 +52,9 @@ import java.util.Set;
* @author kenton@google.com (Kenton Varda)
*/
public class Internal {
+
+ protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
+
/**
* Helper called by generated code to construct default values for string
* fields.
@@ -80,14 +84,7 @@ public class Internal {
* 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), ByteString.UTF_8);
}
/**
@@ -99,14 +96,7 @@ public class Internal {
* embed raw bytes as a string literal with ISO-8859-1 encoding.
*/
public static ByteString bytesDefaultValue(String bytes) {
- try {
- return ByteString.copyFrom(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 ByteString.copyFrom(bytes.getBytes(ISO_8859_1));
}
/**
* Helper called by generated code to construct default values for bytes
@@ -115,14 +105,7 @@ public class Internal {
* This is like {@link #bytesDefaultValue}, but returns a byte array.
*/
public static byte[] byteArrayDefaultValue(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);
}
/**
@@ -161,7 +144,7 @@ public class Internal {
* without loss. More precisely, returns {@code true} whenever:
* <pre> {@code
* Arrays.equals(byteString.toByteArray(),
- * new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8"))
+ * new String(byteString.toByteArray(), ByteString.UTF_8).getBytes(ByteString.UTF_8))
* }</pre>
*
* <p>This method rejects "overlong" byte sequences, as well as
@@ -197,22 +180,14 @@ public class Internal {
* Helper method to get the UTF-8 bytes of a string.
*/
public static byte[] toByteArray(String value) {
- try {
- return value.getBytes("UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("UTF-8 not supported?", e);
- }
+ return value.getBytes(ByteString.UTF_8);
}
/**
* Helper method to convert a byte array to a string using UTF-8 encoding.
*/
public static String toStringUtf8(byte[] bytes) {
- try {
- return new String(bytes, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("UTF-8 not supported?", e);
- }
+ return new String(bytes, ByteString.UTF_8);
}
/**
diff --git a/java/src/main/java/com/google/protobuf/Utf8.java b/java/src/main/java/com/google/protobuf/Utf8.java
index 4d0ef53b..50702410 100644
--- a/java/src/main/java/com/google/protobuf/Utf8.java
+++ b/java/src/main/java/com/google/protobuf/Utf8.java
@@ -46,7 +46,7 @@ package com.google.protobuf;
* <p>The byte sequences considered valid by this class are exactly
* those that can be roundtrip converted to Strings and back to bytes
* using the UTF-8 charset, without loss: <pre> {@code
- * Arrays.equals(bytes, new String(bytes, "UTF-8").getBytes("UTF-8"))
+ * Arrays.equals(bytes, new String(bytes, ByteString.UTF_8).getBytes(ByteString.UTF_8))
* }</pre>
*
* <p>See the Unicode Standard,</br>