From 7139d1eff739682a088ea2c2dbdfef2f108321f8 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 31 Mar 2015 10:29:58 -0700 Subject: [PATCH 1/7] Whitespace --- .../java/com/google/protobuf/ByteString.java | 10 ++-- .../google/protobuf/CodedOutputStream.java | 4 +- .../java/com/google/protobuf/Descriptors.java | 52 +++++++++---------- .../java/com/google/protobuf/Internal.java | 50 +++++++++--------- .../google/protobuf/LiteralByteString.java | 12 ++--- .../com/google/protobuf/ByteStringTest.java | 12 ++--- .../protobuf/CodedOutputStreamTest.java | 10 ++-- .../com/google/protobuf/DescriptorsTest.java | 14 ++--- .../protobuf/LiteralByteStringTest.java | 2 +- .../protobuf/RopeByteStringSubstringTest.java | 2 +- .../google/protobuf/RopeByteStringTest.java | 2 +- .../com/google/protobuf/TextFormatTest.java | 2 +- 12 files changed, 86 insertions(+), 86 deletions(-) diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index 9b0a524b25..637df5f485 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -507,7 +507,7 @@ public abstract class ByteString implements Iterable, Serializable { /** * Internal (package private) implementation of * {@link #copyTo(byte[],int,int,int)}. - * It assumes that all error checking has already been performed and that + * It assumes that all error checking has already been performed and that * {@code numberToCopy > 0}. */ protected abstract void copyToInternal(byte[] target, int sourceOffset, @@ -546,7 +546,7 @@ public abstract class ByteString implements Iterable, Serializable { * @throws IOException if an I/O error occurs. */ public abstract void writeTo(OutputStream out) throws IOException; - + /** * Writes a specified part of this byte string to an output stream. * @@ -572,7 +572,7 @@ public abstract class ByteString implements Iterable, Serializable { if (numberToWrite > 0) { writeToInternal(out, sourceOffset, numberToWrite); } - + } /** @@ -599,7 +599,7 @@ public abstract class ByteString implements Iterable, Serializable { *

* By returning a list, implementations of this method may be able to avoid * copying even when there are multiple backing arrays. - * + * * @return a list of wrapped bytes */ public abstract List asReadOnlyByteBufferList(); @@ -831,7 +831,7 @@ public abstract class ByteString implements Iterable, Serializable { flushLastBuffer(); return ByteString.copyFrom(flushedBuffers); } - + /** * Implement java.util.Arrays.copyOf() for jdk 1.5. */ diff --git a/java/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/src/main/java/com/google/protobuf/CodedOutputStream.java index fafe035689..cb318480ca 100644 --- a/java/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -144,7 +144,7 @@ public final class CodedOutputStream { int bufferSize) { return newInstance(new ByteBufferOutputStream(byteBuffer), bufferSize); } - + private static class ByteBufferOutputStream extends OutputStream { private final ByteBuffer byteBuffer; public ByteBufferOutputStream(ByteBuffer byteBuffer) { @@ -750,7 +750,7 @@ public final class CodedOutputStream { computeUInt32Size(WireFormat.MESSAGE_SET_TYPE_ID, fieldNumber) + computeLazyFieldSize(WireFormat.MESSAGE_SET_MESSAGE, value); } - + // ----------------------------------------------------------------- /** diff --git a/java/src/main/java/com/google/protobuf/Descriptors.java b/java/src/main/java/com/google/protobuf/Descriptors.java index 806f46c1c7..d819a8adad 100644 --- a/java/src/main/java/com/google/protobuf/Descriptors.java +++ b/java/src/main/java/com/google/protobuf/Descriptors.java @@ -256,7 +256,7 @@ public final class Descriptors { throws DescriptorValidationException { return buildFrom(proto, dependencies, false); } - + /** * Construct a {@code FileDescriptor}. @@ -495,9 +495,9 @@ public final class Descriptors { proto.getExtension(i), this, null, i, true); } } - + /** - * Create a placeholder FileDescriptor for a message Descriptor. + * Create a placeholder FileDescriptor for a message Descriptor. */ FileDescriptor(String packageName, Descriptor message) throws DescriptorValidationException { @@ -561,7 +561,7 @@ public final class Descriptors { extensions[i].setProto(proto.getExtension(i)); } } - + boolean supportsUnknownEnumValue() { return getSyntax() == Syntax.PROTO3; } @@ -745,7 +745,7 @@ public final class Descriptors { this.fields = new FieldDescriptor[0]; this.extensions = new FieldDescriptor[0]; this.oneofs = new OneofDescriptor[0]; - + // Create a placeholder FileDescriptor to hold this message. this.file = new FileDescriptor(packageName, this); } @@ -1333,8 +1333,8 @@ public final class Descriptors { "Message type had default value."); } } catch (NumberFormatException e) { - throw new DescriptorValidationException(this, - "Could not parse default value: \"" + + throw new DescriptorValidationException(this, + "Could not parse default value: \"" + proto.getDefaultValue() + '\"', e); } } else { @@ -1456,7 +1456,7 @@ public final class Descriptors { return file.pool.enumValuesByNumber.get( new DescriptorPool.DescriptorIntPair(this, number)); } - + /** * Get the enum value for a number. If no enum value has this number, * construct an EnumValueDescriptor for it. @@ -1494,7 +1494,7 @@ public final class Descriptors { // then remove the whole entry. This way unknown descriptors will // be freed automatically and we don't need to do anything to // clean-up unused map entries. - + // Note: We must use "new Integer(number)" here because we don't want // these Integer objects to be cached. Integer key = new Integer(number); @@ -1509,7 +1509,7 @@ public final class Descriptors { } return result; } - + // Used in tests only. int getUnknownEnumValueDescriptorCount() { return unknownValues.size(); @@ -1585,7 +1585,7 @@ public final class Descriptors { /** Get the value's number. */ public int getNumber() { return proto.getNumber(); } - + @Override public String toString() { return proto.getName(); } @@ -1627,7 +1627,7 @@ public final class Descriptors { file.pool.addSymbol(this); file.pool.addEnumValueByNumber(this); } - + private Integer number; // Create an unknown enum value. private EnumValueDescriptor( @@ -1643,7 +1643,7 @@ public final class Descriptors { this.type = parent; this.fullName = parent.getFullName() + '.' + proto.getName(); this.number = number; - + // Don't add this descriptor into pool. } @@ -1930,13 +1930,13 @@ public final class Descriptors { * descriptors defined in a particular file. */ private static final class DescriptorPool { - - /** Defines what subclass of descriptors to search in the descriptor pool. + + /** Defines what subclass of descriptors to search in the descriptor pool. */ enum SearchFilter { TYPES_ONLY, AGGREGATES_ONLY, ALL_SYMBOLS } - + DescriptorPool(final FileDescriptor[] dependencies, boolean allowUnknownDependencies) { this.dependencies = new HashSet(); @@ -1982,9 +1982,9 @@ public final class Descriptors { GenericDescriptor findSymbol(final String fullName) { return findSymbol(fullName, SearchFilter.ALL_SYMBOLS); } - - /** Find a descriptor by fully-qualified name and given option to only - * search valid field type descriptors. + + /** Find a descriptor by fully-qualified name and given option to only + * search valid field type descriptors. */ GenericDescriptor findSymbol(final String fullName, final SearchFilter filter) { @@ -2013,18 +2013,18 @@ public final class Descriptors { /** Checks if the descriptor is a valid type for a message field. */ boolean isType(GenericDescriptor descriptor) { - return (descriptor instanceof Descriptor) || + return (descriptor instanceof Descriptor) || (descriptor instanceof EnumDescriptor); } - + /** Checks if the descriptor is a valid namespace type. */ boolean isAggregate(GenericDescriptor descriptor) { - return (descriptor instanceof Descriptor) || - (descriptor instanceof EnumDescriptor) || - (descriptor instanceof PackageDescriptor) || + return (descriptor instanceof Descriptor) || + (descriptor instanceof EnumDescriptor) || + (descriptor instanceof PackageDescriptor) || (descriptor instanceof ServiceDescriptor); } - + /** * Look up a type descriptor by name, relative to some other descriptor. * The name may be fully-qualified (with a leading '.'), @@ -2082,7 +2082,7 @@ public final class Descriptors { // Append firstPart and try to find scopeToTry.append(firstPart); - result = findSymbol(scopeToTry.toString(), + result = findSymbol(scopeToTry.toString(), DescriptorPool.SearchFilter.AGGREGATES_ONLY); if (result != null) { diff --git a/java/src/main/java/com/google/protobuf/Internal.java b/java/src/main/java/com/google/protobuf/Internal.java index 5a0de6d1a7..b9a8a994c3 100644 --- a/java/src/main/java/com/google/protobuf/Internal.java +++ b/java/src/main/java/com/google/protobuf/Internal.java @@ -112,7 +112,7 @@ public class Internal { * Helper called by generated code to construct default values for bytes * fields. *

- * This is like {@link #bytesDefaultValue}, but returns a byte array. + * This is like {@link #bytesDefaultValue}, but returns a byte array. */ public static byte[] byteArrayDefaultValue(String bytes) { try { @@ -129,7 +129,7 @@ public class Internal { * Helper called by generated code to construct default values for bytes * fields. *

- * This is like {@link #bytesDefaultValue}, but returns a ByteBuffer. + * This is like {@link #bytesDefaultValue}, but returns a ByteBuffer. */ public static ByteBuffer byteBufferDefaultValue(String bytes) { return ByteBuffer.wrap(byteArrayDefaultValue(bytes)); @@ -185,7 +185,7 @@ public class Internal { public static boolean isValidUtf8(ByteString byteString) { return byteString.isValidUtf8(); } - + /** * Like {@link #isValidUtf8(ByteString)} but for byte arrays. */ @@ -203,7 +203,7 @@ public class Internal { throw new RuntimeException("UTF-8 not supported?", e); } } - + /** * Helper method to convert a byte array to a string using UTF-8 encoding. */ @@ -274,7 +274,7 @@ public class Internal { } return hash; } - + /** * Helper method for implementing {@link Message#equals(Object)} for bytes field. */ @@ -298,7 +298,7 @@ public class Internal { } return hash; } - + /** * Helper method for implementing {@link Message#hashCode()} for bytes field. */ @@ -309,7 +309,7 @@ public class Internal { // based hashCode() method. return LiteralByteString.hashCode(bytes); } - + /** * Helper method for implementing {@link Message#equals(Object)} for bytes * field. @@ -322,7 +322,7 @@ public class Internal { // compare all the content. return a.duplicate().clear().equals(b.duplicate().clear()); } - + /** * Helper method for implementing {@link Message#equals(Object)} for bytes * field. @@ -351,9 +351,9 @@ public class Internal { } return hash; } - + private static final int DEFAULT_BUFFER_SIZE = 4096; - + /** * Helper method for implementing {@link Message#hashCode()} for bytes * field. @@ -382,18 +382,18 @@ public class Internal { return h == 0 ? 1 : h; } } - + /** * An empty byte array constant used in generated code. */ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; - + /** * An empty byte array constant used in generated code. */ public static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(EMPTY_BYTE_ARRAY); - + /** An empty coded input stream constant used in generated code. */ public static final CodedInputStream EMPTY_CODED_INPUT_STREAM = CodedInputStream.newInstance(EMPTY_BYTE_ARRAY); @@ -458,13 +458,13 @@ public class Internal { private final Map realMap; private final Converter valueConverter; - + public MapAdapter(Map realMap, Converter valueConverter) { this.realMap = realMap; this.valueConverter = valueConverter; } - + @SuppressWarnings("unchecked") @Override public V get(Object key) { @@ -474,7 +474,7 @@ public class Internal { } return valueConverter.doForward(result); } - + @Override public V put(K key, V value) { RealValue oldValue = realMap.put(key, valueConverter.doBackward(value)); @@ -488,13 +488,13 @@ public class Internal { public Set> entrySet() { return new SetAdapter(realMap.entrySet()); } - + private class SetAdapter extends AbstractSet> { private final Set> realSet; public SetAdapter(Set> realSet) { this.realSet = realSet; } - + @Override public Iterator> iterator() { return new IteratorAdapter(realSet.iterator()); @@ -503,17 +503,17 @@ public class Internal { @Override public int size() { return realSet.size(); - } + } } - + private class IteratorAdapter implements Iterator> { private final Iterator> realIterator; - + public IteratorAdapter( Iterator> realIterator) { this.realIterator = realIterator; } - + @Override public boolean hasNext() { return realIterator.hasNext(); @@ -529,14 +529,14 @@ public class Internal { realIterator.remove(); } } - + private class EntryAdapter implements Map.Entry { private final Map.Entry realEntry; - + public EntryAdapter(Map.Entry realEntry) { this.realEntry = realEntry; } - + @Override public K getKey() { return realEntry.getKey(); diff --git a/java/src/main/java/com/google/protobuf/LiteralByteString.java b/java/src/main/java/com/google/protobuf/LiteralByteString.java index 6893ddf158..81d8e74bfe 100644 --- a/java/src/main/java/com/google/protobuf/LiteralByteString.java +++ b/java/src/main/java/com/google/protobuf/LiteralByteString.java @@ -112,7 +112,7 @@ class LiteralByteString extends ByteString { // ByteString -> byte[] @Override - protected void copyToInternal(byte[] target, int sourceOffset, + protected void copyToInternal(byte[] target, int sourceOffset, int targetOffset, int numberToCopy) { // Optimized form, not for subclasses, since we don't call // getOffsetIntoBytes() or check the 'numberToCopy' parameter. @@ -199,12 +199,12 @@ class LiteralByteString extends ByteString { LiteralByteString otherAsLiteral = (LiteralByteString) other; // If we know the hash codes and they are not equal, we know the byte // strings are not equal. - if (hash != 0 - && otherAsLiteral.hash != 0 + if (hash != 0 + && otherAsLiteral.hash != 0 && hash != otherAsLiteral.hash) { return false; } - + return equalsRange((LiteralByteString) other, 0, size()); } else if (other instanceof RopeByteString) { return other.equals(this); @@ -285,14 +285,14 @@ class LiteralByteString extends ByteString { protected int partialHash(int h, int offset, int length) { return hashCode(h, bytes, getOffsetIntoBytes() + offset, length); } - + static int hashCode(int h, byte[] bytes, int offset, int length) { for (int i = offset; i < offset + length; i++) { h = h * 31 + bytes[i]; } return h; } - + static int hashCode(byte[] bytes) { int h = hashCode(bytes.length, bytes, 0, bytes.length); return h == 0 ? 1 : h; diff --git a/java/src/test/java/com/google/protobuf/ByteStringTest.java b/java/src/test/java/com/google/protobuf/ByteStringTest.java index 15de17cd31..8d02619056 100644 --- a/java/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/ByteStringTest.java @@ -380,7 +380,7 @@ public class ByteStringTest extends TestCase { return -1; } } - + // A stream which exposes the byte array passed into write(byte[], int, int). private static class EvilOutputStream extends OutputStream { public byte[] capturedArray = null; @@ -492,13 +492,13 @@ public class ByteStringTest extends TestCase { isArrayRange(bytes, byteString.toByteArray(), 0, bytes.length)); } } - + public void testNewOutputEmpty() throws IOException { // Make sure newOutput() correctly builds empty byte strings ByteString byteString = ByteString.newOutput().toByteString(); assertEquals(ByteString.EMPTY, byteString); } - + public void testNewOutput_Mutating() throws IOException { Output os = ByteString.newOutput(5); os.write(new byte[] {1, 2, 3, 4, 5}); @@ -705,14 +705,14 @@ public class ByteStringTest extends TestCase { } return pieces; } - + private byte[] substringUsingWriteTo( ByteString data, int offset, int length) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); data.writeTo(output, offset, length); return output.toByteArray(); } - + public void testWriteToOutputStream() throws Exception { // Choose a size large enough so when two ByteStrings are concatenated they // won't be merged into one byte array due to some optimizations. @@ -727,7 +727,7 @@ public class ByteStringTest extends TestCase { byte[] result = substringUsingWriteTo(left, 1, 1); assertEquals(1, result.length); assertEquals((byte) 11, result[0]); - + byte[] data2 = new byte[dataSize]; for (int i = 0; i < data1.length; i++) { data2[i] = (byte) 2; diff --git a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index 0785cc91a0..ec0c3e1c7b 100644 --- a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -330,7 +330,7 @@ public class CodedOutputStreamTest extends TestCase { assertTrue(codedStream.getTotalBytesWritten() > BUFFER_SIZE); assertEquals(value.length * 1024, codedStream.getTotalBytesWritten()); } - + public void testWriteToByteBuffer() throws Exception { final int bufferSize = 16 * 1024; ByteBuffer buffer = ByteBuffer.allocate(bufferSize); @@ -351,7 +351,7 @@ public class CodedOutputStreamTest extends TestCase { codedStream.writeRawByte((byte) 3); } codedStream.flush(); - + // Check that data is correctly written to the ByteBuffer. assertEquals(0, buffer.remaining()); buffer.flip(); @@ -365,7 +365,7 @@ public class CodedOutputStreamTest extends TestCase { assertEquals((byte) 3, buffer.get()); } } - + public void testWriteByteBuffer() throws Exception { byte[] value = "abcde".getBytes("UTF-8"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -377,10 +377,10 @@ public class CodedOutputStreamTest extends TestCase { // The above call shouldn't affect the ByteBuffer's state. assertEquals(0, byteBuffer.position()); assertEquals(1, byteBuffer.limit()); - + // The correct way to write part of an array using ByteBuffer. codedStream.writeRawBytes(ByteBuffer.wrap(value, 2, 1).slice()); - + codedStream.flush(); byte[] result = outputStream.toByteArray(); assertEquals(6, result.length); diff --git a/java/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/src/test/java/com/google/protobuf/DescriptorsTest.java index 05832a1242..0fe883bba3 100644 --- a/java/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -497,7 +497,7 @@ public class DescriptorsTest extends TestCase { .build(); // translate and crosslink FileDescriptor file = - Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, + Descriptors.FileDescriptor.buildFrom(fileDescriptorProto, new FileDescriptor[0]); // verify resulting descriptors assertNotNull(file); @@ -518,7 +518,7 @@ public class DescriptorsTest extends TestCase { } assertTrue(barFound); } - + public void testDependencyOrder() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto").build(); @@ -537,14 +537,14 @@ public class DescriptorsTest extends TestCase { new FileDescriptor[0]); FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); - - // Items in the FileDescriptor array can be in any order. + + // Items in the FileDescriptor array can be in any order. Descriptors.FileDescriptor.buildFrom(bazProto, new FileDescriptor[] {fooFile, barFile}); Descriptors.FileDescriptor.buildFrom(bazProto, new FileDescriptor[] {barFile, fooFile}); } - + public void testInvalidPublicDependency() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto").build(); @@ -628,7 +628,7 @@ public class DescriptorsTest extends TestCase { Descriptors.FileDescriptor.buildFrom( fooProto, new FileDescriptor[] {forwardFile}); } - + /** * Tests the translate/crosslink for an example with a more complex namespace * referencing. @@ -677,7 +677,7 @@ public class DescriptorsTest extends TestCase { assertTrue(field.getEnumType().getFile().getName().equals("bar.proto")); assertTrue(field.getEnumType().getFile().getPackage().equals( "a.b.c.d.bar.shared")); - } + } } public void testOneofDescriptor() throws Exception { diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java index 046832deb2..f3ad774f25 100644 --- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -248,7 +248,7 @@ public class LiteralByteStringTest extends TestCase { assertTrue(classUnderTest + ".writeTo() must give back the same bytes", Arrays.equals(referenceBytes, roundTripBytes)); } - + public void testWriteTo_mutating() throws IOException { OutputStream os = new OutputStream() { @Override diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java index 212f8d68bb..8106201d33 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java @@ -35,7 +35,7 @@ import java.util.Iterator; /** * This class tests {@link RopeByteString#substring(int, int)} by inheriting the tests from - * {@link LiteralByteStringTest}. Only a couple of methods are overridden. + * {@link LiteralByteStringTest}. Only a couple of methods are overridden. * * @author carlanton@google.com (Carl Haverl) */ diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java index 0f2344d64b..4775f03a8e 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -42,7 +42,7 @@ import java.util.Iterator; /** * This class tests {@link RopeByteString} by inheriting the tests from * {@link LiteralByteStringTest}. Only a couple of methods are overridden. - * + * *

A full test of the result of {@link RopeByteString#substring(int, int)} is found in the * separate class {@link RopeByteStringSubstringTest}. * diff --git a/java/src/test/java/com/google/protobuf/TextFormatTest.java b/java/src/test/java/com/google/protobuf/TextFormatTest.java index eba06ca05c..07c2b833ee 100644 --- a/java/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/src/test/java/com/google/protobuf/TextFormatTest.java @@ -863,7 +863,7 @@ public class TextFormatTest extends TestCase { TextFormat.merge(TextFormat.printToUnicodeString(message), builder); assertEquals(message.getOptionalString(), builder.getOptionalString()); } - + public void testPrintToUnicodeStringWithNewlines() throws Exception { // No newlines at start and end assertEquals("optional_string: \"test newlines\\n\\nin\\nstring\"\n", From e84893f6768f136cc86e2db69fc1d40ff2be7e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Szathma=CC=81ry?= Date: Tue, 9 Sep 2014 16:31:51 +0200 Subject: [PATCH 2/7] perf: String#getBytes(Charset) vs getBytes(String) --- .../java/com/google/protobuf/ByteString.java | 49 +++++++++++++------ .../google/protobuf/LiteralByteString.java | 10 ++-- .../com/google/protobuf/RopeByteString.java | 10 ++-- .../protobuf/BoundedByteStringTest.java | 13 +++++ .../protobuf/LiteralByteStringTest.java | 7 +++ .../protobuf/RopeByteStringSubstringTest.java | 30 ++++++++++++ .../google/protobuf/RopeByteStringTest.java | 26 ++++++++++ 7 files changed, 117 insertions(+), 28 deletions(-) diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index 637df5f485..ee2eddb082 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -37,6 +37,8 @@ import java.io.OutputStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -76,8 +78,7 @@ public abstract class ByteString implements Iterable, Serializable { static final int MIN_READ_FROM_CHUNK_SIZE = 0x100; // 256b static final int MAX_READ_FROM_CHUNK_SIZE = 0x2000; // 8k - // Defined by java.nio.charset.Charset - protected static final String UTF_8 = "UTF-8"; + protected static final Charset UTF_8 = Charset.forName("UTF-8"); /** * Empty {@code ByteString}. @@ -269,11 +270,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new {@code ByteString} */ public static ByteString copyFromUtf8(String text) { - try { - return new LiteralByteString(text.getBytes(UTF_8)); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 not supported?", e); - } + return new LiteralByteString(text.getBytes(UTF_8)); } // ================================================================= @@ -612,8 +609,36 @@ public abstract class ByteString implements Iterable, Serializable { * @return new string * @throws UnsupportedEncodingException if charset isn't recognized */ - public abstract String toString(String charsetName) - throws UnsupportedEncodingException; + public String toString(String charsetName) + throws UnsupportedEncodingException { + try { + return toString(Charset.forName(charsetName)); + } catch (UnsupportedCharsetException e) { + UnsupportedEncodingException exception = new UnsupportedEncodingException(charsetName); + exception.initCause(e); + throw exception; + } + } + + /** + * Constructs a new {@code String} by decoding the bytes using the + * specified charset. Returns the same empty String if empty. + * + * @param charset encode using this charset + * @return new string + */ + public String toString(Charset charset) { + return size() == 0 ? "" : toStringInternal(charset); + } + + /** + * Constructs a new {@code String} by decoding the bytes using the + * specified charset. + * + * @param charset encode using this charset + * @return new string + */ + protected abstract String toStringInternal(Charset charset); // ================================================================= // UTF-8 decoding @@ -624,11 +649,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new string using UTF-8 encoding */ public String toStringUtf8() { - try { - return toString(UTF_8); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 not supported?", e); - } + return toString(UTF_8); } /** diff --git a/java/src/main/java/com/google/protobuf/LiteralByteString.java b/java/src/main/java/com/google/protobuf/LiteralByteString.java index 81d8e74bfe..3462c3957b 100644 --- a/java/src/main/java/com/google/protobuf/LiteralByteString.java +++ b/java/src/main/java/com/google/protobuf/LiteralByteString.java @@ -36,6 +36,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.NoSuchElementException; @@ -152,13 +153,8 @@ class LiteralByteString extends ByteString { } @Override - public String toString(String charsetName) - throws UnsupportedEncodingException { - // 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); + protected String toStringInternal(Charset charset) { + return new String(bytes, getOffsetIntoBytes(), size(), charset); } // ================================================================= diff --git a/java/src/main/java/com/google/protobuf/RopeByteString.java b/java/src/main/java/com/google/protobuf/RopeByteString.java index 168bcce229..0900a04208 100644 --- a/java/src/main/java/com/google/protobuf/RopeByteString.java +++ b/java/src/main/java/com/google/protobuf/RopeByteString.java @@ -38,6 +38,7 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.io.ByteArrayInputStream; import java.nio.ByteBuffer; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -418,13 +419,8 @@ class RopeByteString extends ByteString { } @Override - public String toString(String charsetName) - throws UnsupportedEncodingException { - // Optimize for empty strings, but ensure we don't silently ignore invalid - // encodings. - return size() == 0 && UTF_8.equals(charsetName) - ? "" - : new String(toByteArray(), charsetName); + protected String toStringInternal(Charset charset) { + return new String(toByteArray(), charset); } // ================================================================= diff --git a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java index 6c9596ca64..a11bef2e87 100644 --- a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -72,6 +72,19 @@ public class BoundedByteStringTest extends LiteralByteStringTest { testString.substring(2, testString.length() - 6), roundTripString); } + @Override + public void testCharsetToString() throws UnsupportedEncodingException { + String testString = "I love unicode \u1234\u5678 characters"; + LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + ByteString chopped = unicode.substring(2, unicode.size() - 6); + assertEquals(classUnderTest + ".substring() must have the expected type", + classUnderTest, getActualClassName(chopped)); + + String roundTripString = chopped.toString(ByteString.UTF_8); + assertEquals(classUnderTest + " unicode bytes must match", + testString.substring(2, testString.length() - 6), roundTripString); + } + public void testJavaSerialization() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(out); diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java index f3ad774f25..8607040e61 100644 --- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -298,6 +298,13 @@ public class LiteralByteStringTest extends TestCase { assertEquals(classUnderTest + " unicode must match", testString, roundTripString); } + public void testCharsetToString() throws UnsupportedEncodingException { + String testString = "I love unicode \u1234\u5678 characters"; + LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + String roundTripString = unicode.toString(ByteString.UTF_8); + assertEquals(classUnderTest + " unicode must match", testString, roundTripString); + } + public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException{ assertSame(classUnderTest + " must be the same string references", ByteString.EMPTY.toString(UTF_8), new LiteralByteString(new byte[]{}).toString(UTF_8)); diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java index 8106201d33..43872d1d23 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java @@ -94,4 +94,34 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest { assertEquals(classUnderTest + " string must must have same hashCode as the flat string", flatString.hashCode(), unicode.hashCode()); } + + @Override + public void testCharsetToString() throws UnsupportedEncodingException { + String sourceString = "I love unicode \u1234\u5678 characters"; + ByteString sourceByteString = ByteString.copyFromUtf8(sourceString); + int copies = 250; + + // By building the RopeByteString by concatenating, this is actually a fairly strenuous test. + StringBuilder builder = new StringBuilder(copies * sourceString.length()); + ByteString unicode = ByteString.EMPTY; + for (int i = 0; i < copies; ++i) { + builder.append(sourceString); + unicode = RopeByteString.concatenate(unicode, sourceByteString); + } + String testString = builder.toString(); + + // Do the substring part + testString = testString.substring(2, testString.length() - 6); + unicode = unicode.substring(2, unicode.size() - 6); + + assertEquals(classUnderTest + " from string must have the expected type", + classUnderTest, getActualClassName(unicode)); + String roundTripString = unicode.toString(ByteString.UTF_8); + assertEquals(classUnderTest + " unicode bytes must match", + testString, roundTripString); + ByteString flatString = ByteString.copyFromUtf8(testString); + assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); + assertEquals(classUnderTest + " string must must have same hashCode as the flat string", + flatString.hashCode(), unicode.hashCode()); + } } diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java index 4775f03a8e..54eb96834a 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -118,6 +118,32 @@ public class RopeByteStringTest extends LiteralByteStringTest { flatString.hashCode(), unicode.hashCode()); } + @Override + public void testCharsetToString() throws UnsupportedEncodingException { + String sourceString = "I love unicode \u1234\u5678 characters"; + ByteString sourceByteString = ByteString.copyFromUtf8(sourceString); + int copies = 250; + + // By building the RopeByteString by concatenating, this is actually a fairly strenuous test. + StringBuilder builder = new StringBuilder(copies * sourceString.length()); + ByteString unicode = ByteString.EMPTY; + for (int i = 0; i < copies; ++i) { + builder.append(sourceString); + unicode = RopeByteString.concatenate(unicode, sourceByteString); + } + String testString = builder.toString(); + + assertEquals(classUnderTest + " from string must have the expected type", + classUnderTest, getActualClassName(unicode)); + String roundTripString = unicode.toString(ByteString.UTF_8); + assertEquals(classUnderTest + " unicode bytes must match", + testString, roundTripString); + ByteString flatString = ByteString.copyFromUtf8(testString); + assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); + assertEquals(classUnderTest + " string must must have same hashCode as the flat string", + flatString.hashCode(), unicode.hashCode()); + } + @Override public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException { RopeByteString ropeByteString = From 210de285d7ca5e4d4e96867c1c04308d68c5d4dd Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 31 Mar 2015 20:01:43 -0700 Subject: [PATCH 3/7] DRY: Use `Charset` statics to eliminate exceptions --- .../java/com/google/protobuf/ByteString.java | 12 +++++ .../com/google/protobuf/CodedInputStream.java | 6 +-- .../google/protobuf/CodedOutputStream.java | 12 ++--- .../java/com/google/protobuf/Descriptors.java | 7 +-- .../java/com/google/protobuf/Internal.java | 45 +++++-------------- .../main/java/com/google/protobuf/Utf8.java | 2 +- .../protobuf/BoundedByteStringTest.java | 2 +- .../com/google/protobuf/ByteStringTest.java | 9 ++-- .../protobuf/CodedOutputStreamTest.java | 4 +- .../com/google/protobuf/DescriptorsTest.java | 2 +- .../google/protobuf/IsValidUtf8TestUtil.java | 8 ++-- .../protobuf/LiteralByteStringTest.java | 4 +- .../google/protobuf/RopeByteStringTest.java | 2 +- .../java/com/google/protobuf/TestUtil.java | 6 +-- .../com/google/protobuf/TextFormatTest.java | 4 +- .../protobuf/UnknownFieldSetLiteTest.java | 2 +- .../nano/CodedInputByteBufferNano.java | 4 +- .../nano/CodedOutputByteBufferNano.java | 12 ++--- .../google/protobuf/nano/InternalNano.java | 29 +++--------- .../com/google/protobuf/nano/NanoTest.java | 22 ++++----- 20 files changed, 75 insertions(+), 119 deletions(-) diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index ee2eddb082..04ac7c9a16 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -262,6 +262,18 @@ public abstract class ByteString implements Iterable, Serializable { return new LiteralByteString(text.getBytes(charsetName)); } + /** + * 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 0ca00babfd..b15c27310f 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 cb318480ca..9994fd3ecf 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 d819a8adad..ab8930a1f8 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 b9a8a994c3..8a8bcda146 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: *

   {@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))
    * }
* *

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 4d0ef53ba9..507024101e 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; *

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:

 {@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))
  * }
* *

See the Unicode Standard,
diff --git a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java index a11bef2e87..9c0ff9251e 100644 --- a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest { @Override public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertEquals(classUnderTest + ".substring() must have the expected type", classUnderTest, getActualClassName(chopped)); diff --git a/java/src/test/java/com/google/protobuf/ByteStringTest.java b/java/src/test/java/com/google/protobuf/ByteStringTest.java index 8d02619056..2eb890d5b6 100644 --- a/java/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/ByteStringTest.java @@ -36,11 +36,12 @@ import junit.framework.TestCase; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.InputStream; +import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -56,7 +57,7 @@ import java.util.Random; */ public class ByteStringTest extends TestCase { - private static final String UTF_16 = "UTF-16"; + private static final Charset UTF_16 = Charset.forName("UTF-16"); static byte[] getTestBytes(int size, long seed) { Random random = new Random(seed); @@ -139,7 +140,7 @@ public class ByteStringTest extends TestCase { public void testCopyFrom_Utf8() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; ByteString byteString = ByteString.copyFromUtf8(testString); - byte[] testBytes = testString.getBytes("UTF-8"); + byte[] testBytes = testString.getBytes(ByteString.UTF_8); assertTrue("copyFromUtf8 string must respect the charset", isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); } @@ -400,7 +401,7 @@ public class ByteStringTest extends TestCase { public void testToStringUtf8() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - byte[] testBytes = testString.getBytes("UTF-8"); + byte[] testBytes = testString.getBytes(ByteString.UTF_8); ByteString byteString = ByteString.copyFrom(testBytes); assertEquals("copyToStringUtf8 must respect the charset", testString, byteString.toStringUtf8()); diff --git a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index ec0c3e1c7b..bb3b03d591 100644 --- a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -321,7 +321,7 @@ public class CodedOutputStreamTest extends TestCase { final int BUFFER_SIZE = 4 * 1024; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); - byte[] value = "abcde".getBytes("UTF-8"); + byte[] value = "abcde".getBytes(ByteString.UTF_8); for (int i = 0; i < 1024; ++i) { codedStream.writeRawBytes(value, 0, value.length); } @@ -367,7 +367,7 @@ public class CodedOutputStreamTest extends TestCase { } public void testWriteByteBuffer() throws Exception { - byte[] value = "abcde".getBytes("UTF-8"); + byte[] value = "abcde".getBytes(ByteString.UTF_8); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); ByteBuffer byteBuffer = ByteBuffer.wrap(value, 0, 1); diff --git a/java/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/src/test/java/com/google/protobuf/DescriptorsTest.java index 0fe883bba3..fee9ca0443 100644 --- a/java/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -286,7 +286,7 @@ public class DescriptorsTest extends TestCase { d = TestExtremeDefaultValues.getDescriptor(); assertEquals( ByteString.copyFrom( - "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes("ISO-8859-1")), + "\0\001\007\b\f\n\r\t\013\\\'\"\u00fe".getBytes(Internal.ISO_8859_1)), d.findFieldByName("escaped_bytes").getDefaultValue()); assertEquals(-1, d.findFieldByName("large_uint32").getDefaultValue()); assertEquals(-1L, d.findFieldByName("large_uint64").getDefaultValue()); diff --git a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java index f41595ec91..d034c0679d 100644 --- a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java +++ b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java @@ -220,8 +220,8 @@ class IsValidUtf8TestUtil { } ByteString bs = ByteString.copyFrom(bytes); boolean isRoundTrippable = bs.isValidUtf8(); - String s = new String(bytes, "UTF-8"); - byte[] bytesReencoded = s.getBytes("UTF-8"); + String s = new String(bytes, ByteString.UTF_8); + byte[] bytesReencoded = s.getBytes(ByteString.UTF_8); boolean bytesEqual = Arrays.equals(bytes, bytesReencoded); if (bytesEqual != isRoundTrippable) { @@ -313,10 +313,10 @@ class IsValidUtf8TestUtil { void testBytesUsingByteBuffers( int numBytes, long expectedCount, long start, long lim) throws UnsupportedEncodingException { - CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder() + CharsetDecoder decoder = ByteString.UTF_8.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); - CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder() + CharsetEncoder encoder = ByteString.UTF_8.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); byte[] bytes = new byte[numBytes]; diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java index 8607040e61..4974e9f708 100644 --- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -293,7 +293,7 @@ public class LiteralByteStringTest extends TestCase { public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); String roundTripString = unicode.toString(UTF_8); assertEquals(classUnderTest + " unicode must match", testString, roundTripString); } @@ -307,7 +307,7 @@ public class LiteralByteStringTest extends TestCase { public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException{ assertSame(classUnderTest + " must be the same string references", - ByteString.EMPTY.toString(UTF_8), new LiteralByteString(new byte[]{}).toString(UTF_8)); + ByteString.EMPTY.toString(ByteString.UTF_8), new LiteralByteString(new byte[]{}).toString(ByteString.UTF_8)); } public void testToString_raisesException() throws UnsupportedEncodingException{ diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java index 54eb96834a..e43df16525 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -149,7 +149,7 @@ public class RopeByteStringTest extends LiteralByteStringTest { RopeByteString ropeByteString = RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); assertSame(classUnderTest + " must be the same string references", - ByteString.EMPTY.toString(UTF_8), ropeByteString.toString(UTF_8)); + ByteString.EMPTY.toString(ByteString.UTF_8), ropeByteString.toString(ByteString.UTF_8)); } public void testToString_raisesException() throws UnsupportedEncodingException{ diff --git a/java/src/test/java/com/google/protobuf/TestUtil.java b/java/src/test/java/com/google/protobuf/TestUtil.java index 135a11740b..0ce72b5ee4 100644 --- a/java/src/test/java/com/google/protobuf/TestUtil.java +++ b/java/src/test/java/com/google/protobuf/TestUtil.java @@ -276,11 +276,7 @@ public final class TestUtil { /** Helper to convert a String to ByteString. */ static ByteString toBytes(String str) { - try { - return ByteString.copyFrom(str.getBytes("UTF-8")); - } catch(java.io.UnsupportedEncodingException e) { - throw new RuntimeException("UTF-8 not supported.", e); - } + return ByteString.copyFrom(str.getBytes(ByteString.UTF_8)); } /** diff --git a/java/src/test/java/com/google/protobuf/TextFormatTest.java b/java/src/test/java/com/google/protobuf/TextFormatTest.java index 07c2b833ee..5d8466465b 100644 --- a/java/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/src/test/java/com/google/protobuf/TextFormatTest.java @@ -243,8 +243,8 @@ public class TextFormatTest extends TestCase { * characters. The characters are converted directly to bytes, *not* * encoded using UTF-8. */ - private ByteString bytes(String str) throws Exception { - return ByteString.copyFrom(str.getBytes("ISO-8859-1")); + private ByteString bytes(String str) { + return ByteString.copyFrom(str.getBytes(Internal.ISO_8859_1)); } /** diff --git a/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java index cec3da1e85..2f6bcd1a0c 100644 --- a/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java +++ b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java @@ -229,7 +229,7 @@ public class UnknownFieldSetLiteTest extends TestCase { public void testMalformedBytes() throws Exception { try { - Foo.parseFrom("this is a malformed protocol buffer".getBytes("UTF-8")); + Foo.parseFrom("this is a malformed protocol buffer".getBytes(ByteString.UTF_8)); fail(); } catch (InvalidProtocolBufferException e) { // Expected. diff --git a/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java b/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java index b4f20fde71..4b45c6d298 100644 --- a/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java +++ b/javanano/src/main/java/com/google/protobuf/nano/CodedInputByteBufferNano.java @@ -190,12 +190,12 @@ public final class CodedInputByteBufferNano { 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, InternalNano.UTF_8); bufferPos += size; return result; } else { // Slow path: Build a byte array first then copy it. - return new String(readRawBytes(size), "UTF-8"); + return new String(readRawBytes(size), InternalNano.UTF_8); } } diff --git a/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java index 2777f34cca..1f0534b173 100644 --- a/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java +++ b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java @@ -291,7 +291,7 @@ public final class CodedOutputByteBufferNano { // 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(InternalNano.UTF_8); writeRawVarint32(bytes.length); writeRawBytes(bytes); } @@ -603,13 +603,9 @@ public final class CodedOutputByteBufferNano { * {@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."); - } + final byte[] bytes = value.getBytes(InternalNano.UTF_8); + return computeRawVarint32Size(bytes.length) + + bytes.length; } /** 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 4a08bb589a..67404d27b5 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); } /** diff --git a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java index 4cc7722621..7de84310c0 100644 --- a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java +++ b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java @@ -458,7 +458,7 @@ public class NanoTest extends TestCase { assertFalse(msg.optionalBytes.length > 0); msg.optionalBytes = InternalNano.copyFromUtf8("hello"); assertTrue(msg.optionalBytes.length > 0); - assertEquals("hello", new String(msg.optionalBytes, "UTF-8")); + assertEquals("hello", new String(msg.optionalBytes, InternalNano.UTF_8)); msg.clear(); assertFalse(msg.optionalBytes.length > 0); msg.clear() @@ -476,7 +476,7 @@ public class NanoTest extends TestCase { TestAllTypesNano newMsg = TestAllTypesNano.parseFrom(result); assertTrue(newMsg.optionalBytes.length > 0); - assertEquals("bye", new String(newMsg.optionalBytes, "UTF-8")); + assertEquals("bye", new String(newMsg.optionalBytes, InternalNano.UTF_8)); } public void testNanoOptionalGroup() throws Exception { @@ -1346,14 +1346,14 @@ public class NanoTest extends TestCase { InternalNano.copyFromUtf8("bye"), InternalNano.copyFromUtf8("boo") }; - assertEquals("bye", new String(msg.repeatedBytes[1], "UTF-8")); - assertEquals("boo", new String(msg.repeatedBytes[2], "UTF-8")); + assertEquals("bye", new String(msg.repeatedBytes[1], InternalNano.UTF_8)); + assertEquals("boo", new String(msg.repeatedBytes[2], InternalNano.UTF_8)); msg.clear(); assertEquals(0, msg.repeatedBytes.length); msg.clear() .repeatedBytes = new byte[][] { InternalNano.copyFromUtf8("boo") }; assertEquals(1, msg.repeatedBytes.length); - assertEquals("boo", new String(msg.repeatedBytes[0], "UTF-8")); + assertEquals("boo", new String(msg.repeatedBytes[0], InternalNano.UTF_8)); msg.clear(); assertEquals(0, msg.repeatedBytes.length); @@ -1385,8 +1385,8 @@ public class NanoTest extends TestCase { newMsg = TestAllTypesNano.parseFrom(result); assertEquals(2, newMsg.repeatedBytes.length); - assertEquals("hello", new String(newMsg.repeatedBytes[0], "UTF-8")); - assertEquals("world", new String(newMsg.repeatedBytes[1], "UTF-8")); + assertEquals("hello", new String(newMsg.repeatedBytes[0], InternalNano.UTF_8)); + assertEquals("world", new String(newMsg.repeatedBytes[1], InternalNano.UTF_8)); } public void testNanoRepeatedGroup() throws Exception { @@ -2277,9 +2277,9 @@ public class NanoTest extends TestCase { assertTrue(52.0e3 == msg.defaultDouble); assertEquals(true, msg.defaultBool); assertEquals("hello", msg.defaultString); - assertEquals("world", new String(msg.defaultBytes, "UTF-8")); + assertEquals("world", new String(msg.defaultBytes, InternalNano.UTF_8)); assertEquals("dünya", msg.defaultStringNonascii); - assertEquals("dünyab", new String(msg.defaultBytesNonascii, "UTF-8")); + assertEquals("dünyab", new String(msg.defaultBytesNonascii, InternalNano.UTF_8)); assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum); assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum); assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum); @@ -2385,7 +2385,7 @@ public class NanoTest extends TestCase { assertEquals(TestAllTypesNanoHas.FOO, newMsg.optionalNestedEnum); assertEquals(41, newMsg.defaultInt32); assertEquals("hello", newMsg.defaultString); - assertEquals("world", new String(newMsg.defaultBytes, "UTF-8")); + assertEquals("world", new String(newMsg.defaultBytes, InternalNano.UTF_8)); assertEquals(TestAllTypesNanoHas.BAR, newMsg.defaultNestedEnum); assertEquals(Float.NaN, newMsg.defaultFloatNan); assertEquals(0, newMsg.id); @@ -2567,7 +2567,7 @@ public class NanoTest extends TestCase { assertEquals(TestNanoAccessors.FOO, newMsg.getOptionalNestedEnum()); assertEquals(41, newMsg.getDefaultInt32()); assertEquals("hello", newMsg.getDefaultString()); - assertEquals("world", new String(newMsg.getDefaultBytes(), "UTF-8")); + assertEquals("world", new String(newMsg.getDefaultBytes(), InternalNano.UTF_8)); assertEquals(TestNanoAccessors.BAR, newMsg.getDefaultNestedEnum()); assertEquals(Float.NaN, newMsg.getDefaultFloatNan()); assertEquals(0, newMsg.id); From 9a2f1f64eb978a6bfbff2196a777cb1dd9bde6c5 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 31 Mar 2015 11:25:39 -0700 Subject: [PATCH 4/7] Run `java{,nano}` tests in CI --- .travis.yml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1811487888..c7834924ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,16 @@ sudo: false -language: cpp +language: java +jdk: + - openjdk6 + - openjdk7 + - oraclejdk7 os: - linux - osx -script: ./autogen.sh && ./configure && make distcheck -j2 +script: + - ./autogen.sh && ./configure && make -j2 + - cd java && mvn test && cd .. + - cd javanano && mvn test && cd .. + - make distcheck -j2 notifications: email: false From 2820e86aab2417a8274dc64a6d5de62f1fcaedc8 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 31 Mar 2015 20:28:27 -0700 Subject: [PATCH 5/7] Skip 3-byte test in CI --- .../src/test/java/com/google/protobuf/IsValidUtf8Test.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/java/src/test/java/com/google/protobuf/IsValidUtf8Test.java b/java/src/test/java/com/google/protobuf/IsValidUtf8Test.java index 89b6451926..8751baaeaa 100644 --- a/java/src/test/java/com/google/protobuf/IsValidUtf8Test.java +++ b/java/src/test/java/com/google/protobuf/IsValidUtf8Test.java @@ -72,8 +72,11 @@ public class IsValidUtf8Test extends TestCase { * Tests that round tripping of all three byte permutations work. */ public void testIsValidUtf8_3Bytes() throws UnsupportedEncodingException { - IsValidUtf8TestUtil.testBytes(3, - IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT); + // Travis' OOM killer doesn't like this test + if (System.getenv("TRAVIS") == null) { + IsValidUtf8TestUtil.testBytes(3, + IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT); + } } /** From badef1fc19566745e1a052430202bb34b6a6150f Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 2 Apr 2015 14:22:35 -0700 Subject: [PATCH 6/7] Move `UTF_8` to `Internal` --- java/src/main/java/com/google/protobuf/ByteString.java | 6 ++---- .../main/java/com/google/protobuf/CodedInputStream.java | 6 +++--- .../main/java/com/google/protobuf/CodedOutputStream.java | 4 ++-- java/src/main/java/com/google/protobuf/Internal.java | 9 +++++---- java/src/main/java/com/google/protobuf/Utf8.java | 2 +- .../java/com/google/protobuf/BoundedByteStringTest.java | 6 +++--- .../test/java/com/google/protobuf/ByteStringTest.java | 4 ++-- .../java/com/google/protobuf/CodedOutputStreamTest.java | 4 ++-- .../java/com/google/protobuf/IsValidUtf8TestUtil.java | 8 ++++---- .../java/com/google/protobuf/LiteralByteStringTest.java | 8 ++++---- .../com/google/protobuf/RopeByteStringSubstringTest.java | 2 +- .../java/com/google/protobuf/RopeByteStringTest.java | 4 ++-- java/src/test/java/com/google/protobuf/TestUtil.java | 2 +- .../com/google/protobuf/UnknownFieldSetLiteTest.java | 2 +- 14 files changed, 33 insertions(+), 34 deletions(-) diff --git a/java/src/main/java/com/google/protobuf/ByteString.java b/java/src/main/java/com/google/protobuf/ByteString.java index 04ac7c9a16..1d5d4e8a5e 100644 --- a/java/src/main/java/com/google/protobuf/ByteString.java +++ b/java/src/main/java/com/google/protobuf/ByteString.java @@ -78,8 +78,6 @@ public abstract class ByteString implements Iterable, Serializable { static final int MIN_READ_FROM_CHUNK_SIZE = 0x100; // 256b static final int MAX_READ_FROM_CHUNK_SIZE = 0x2000; // 8k - protected static final Charset UTF_8 = Charset.forName("UTF-8"); - /** * Empty {@code ByteString}. */ @@ -282,7 +280,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new {@code ByteString} */ public static ByteString copyFromUtf8(String text) { - return new LiteralByteString(text.getBytes(UTF_8)); + return new LiteralByteString(text.getBytes(Internal.UTF_8)); } // ================================================================= @@ -661,7 +659,7 @@ public abstract class ByteString implements Iterable, Serializable { * @return new string using UTF-8 encoding */ public String toStringUtf8() { - return toString(UTF_8); + return toString(Internal.UTF_8); } /** diff --git a/java/src/main/java/com/google/protobuf/CodedInputStream.java b/java/src/main/java/com/google/protobuf/CodedInputStream.java index b15c27310f..d201f7c558 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, ByteString.UTF_8); + final String result = new String(buffer, bufferPos, size, Internal.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), ByteString.UTF_8); + return new String(readRawBytesSlowPath(size), Internal.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, ByteString.UTF_8); + return new String(bytes, pos, size, Internal.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 9994fd3ecf..f5fd58f9af 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(ByteString.UTF_8); + final byte[] bytes = value.getBytes(Internal.UTF_8); writeRawVarint32(bytes.length); writeRawBytes(bytes); } @@ -827,7 +827,7 @@ public final class CodedOutputStream { * {@code string} field. */ public static int computeStringSizeNoTag(final String value) { - final byte[] bytes = value.getBytes(ByteString.UTF_8); + final byte[] bytes = value.getBytes(Internal.UTF_8); return computeRawVarint32Size(bytes.length) + bytes.length; } diff --git a/java/src/main/java/com/google/protobuf/Internal.java b/java/src/main/java/com/google/protobuf/Internal.java index 8a8bcda146..b49318ecee 100644 --- a/java/src/main/java/com/google/protobuf/Internal.java +++ b/java/src/main/java/com/google/protobuf/Internal.java @@ -53,6 +53,7 @@ import java.util.Set; */ public class Internal { + protected static final Charset UTF_8 = Charset.forName("UTF-8"); protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); /** @@ -84,7 +85,7 @@ public class Internal { * generated code calls this automatically. */ public static String stringDefaultValue(String bytes) { - return new String(bytes.getBytes(ISO_8859_1), ByteString.UTF_8); + return new String(bytes.getBytes(ISO_8859_1), UTF_8); } /** @@ -144,7 +145,7 @@ public class Internal { * without loss. More precisely, returns {@code true} whenever: *

   {@code
    * Arrays.equals(byteString.toByteArray(),
-   *     new String(byteString.toByteArray(), ByteString.UTF_8).getBytes(ByteString.UTF_8))
+   *     new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8"))
    * }
* *

This method rejects "overlong" byte sequences, as well as @@ -180,14 +181,14 @@ public class Internal { * Helper method to get the UTF-8 bytes of a string. */ public static byte[] toByteArray(String value) { - return value.getBytes(ByteString.UTF_8); + return value.getBytes(UTF_8); } /** * Helper method to convert a byte array to a string using UTF-8 encoding. */ public static String toStringUtf8(byte[] bytes) { - return new String(bytes, ByteString.UTF_8); + return new String(bytes, 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 507024101e..4271b41bc9 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; *

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:

 {@code
- * Arrays.equals(bytes, new String(bytes, ByteString.UTF_8).getBytes(ByteString.UTF_8))
+ * Arrays.equals(bytes, new String(bytes, Internal.UTF_8).getBytes(Internal.UTF_8))
  * }
* *

See the Unicode Standard,
diff --git a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java index 9c0ff9251e..1562a1a604 100644 --- a/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/BoundedByteStringTest.java @@ -62,7 +62,7 @@ public class BoundedByteStringTest extends LiteralByteStringTest { @Override public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertEquals(classUnderTest + ".substring() must have the expected type", classUnderTest, getActualClassName(chopped)); @@ -75,12 +75,12 @@ public class BoundedByteStringTest extends LiteralByteStringTest { @Override public void testCharsetToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); ByteString chopped = unicode.substring(2, unicode.size() - 6); assertEquals(classUnderTest + ".substring() must have the expected type", classUnderTest, getActualClassName(chopped)); - String roundTripString = chopped.toString(ByteString.UTF_8); + String roundTripString = chopped.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode bytes must match", testString.substring(2, testString.length() - 6), roundTripString); } diff --git a/java/src/test/java/com/google/protobuf/ByteStringTest.java b/java/src/test/java/com/google/protobuf/ByteStringTest.java index 2eb890d5b6..46c229ad9f 100644 --- a/java/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/ByteStringTest.java @@ -140,7 +140,7 @@ public class ByteStringTest extends TestCase { public void testCopyFrom_Utf8() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; ByteString byteString = ByteString.copyFromUtf8(testString); - byte[] testBytes = testString.getBytes(ByteString.UTF_8); + byte[] testBytes = testString.getBytes(Internal.UTF_8); assertTrue("copyFromUtf8 string must respect the charset", isArrayRange(byteString.toByteArray(), testBytes, 0, testBytes.length)); } @@ -401,7 +401,7 @@ public class ByteStringTest extends TestCase { public void testToStringUtf8() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - byte[] testBytes = testString.getBytes(ByteString.UTF_8); + byte[] testBytes = testString.getBytes(Internal.UTF_8); ByteString byteString = ByteString.copyFrom(testBytes); assertEquals("copyToStringUtf8 must respect the charset", testString, byteString.toStringUtf8()); diff --git a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index bb3b03d591..365789c043 100644 --- a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -321,7 +321,7 @@ public class CodedOutputStreamTest extends TestCase { final int BUFFER_SIZE = 4 * 1024; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); - byte[] value = "abcde".getBytes(ByteString.UTF_8); + byte[] value = "abcde".getBytes(Internal.UTF_8); for (int i = 0; i < 1024; ++i) { codedStream.writeRawBytes(value, 0, value.length); } @@ -367,7 +367,7 @@ public class CodedOutputStreamTest extends TestCase { } public void testWriteByteBuffer() throws Exception { - byte[] value = "abcde".getBytes(ByteString.UTF_8); + byte[] value = "abcde".getBytes(Internal.UTF_8); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CodedOutputStream codedStream = CodedOutputStream.newInstance(outputStream); ByteBuffer byteBuffer = ByteBuffer.wrap(value, 0, 1); diff --git a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java index d034c0679d..320370396f 100644 --- a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java +++ b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java @@ -220,8 +220,8 @@ class IsValidUtf8TestUtil { } ByteString bs = ByteString.copyFrom(bytes); boolean isRoundTrippable = bs.isValidUtf8(); - String s = new String(bytes, ByteString.UTF_8); - byte[] bytesReencoded = s.getBytes(ByteString.UTF_8); + String s = new String(bytes, Internal.UTF_8); + byte[] bytesReencoded = s.getBytes(Internal.UTF_8); boolean bytesEqual = Arrays.equals(bytes, bytesReencoded); if (bytesEqual != isRoundTrippable) { @@ -313,10 +313,10 @@ class IsValidUtf8TestUtil { void testBytesUsingByteBuffers( int numBytes, long expectedCount, long start, long lim) throws UnsupportedEncodingException { - CharsetDecoder decoder = ByteString.UTF_8.newDecoder() + CharsetDecoder decoder = Internal.UTF_8.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); - CharsetEncoder encoder = ByteString.UTF_8.newEncoder() + CharsetEncoder encoder = Internal.UTF_8.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE); byte[] bytes = new byte[numBytes]; diff --git a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java index 4974e9f708..7b201a9deb 100644 --- a/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -293,21 +293,21 @@ public class LiteralByteStringTest extends TestCase { public void testToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); String roundTripString = unicode.toString(UTF_8); assertEquals(classUnderTest + " unicode must match", testString, roundTripString); } public void testCharsetToString() throws UnsupportedEncodingException { String testString = "I love unicode \u1234\u5678 characters"; - LiteralByteString unicode = new LiteralByteString(testString.getBytes(ByteString.UTF_8)); - String roundTripString = unicode.toString(ByteString.UTF_8); + LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); + String roundTripString = unicode.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode must match", testString, roundTripString); } public void testToString_returnsCanonicalEmptyString() throws UnsupportedEncodingException{ assertSame(classUnderTest + " must be the same string references", - ByteString.EMPTY.toString(ByteString.UTF_8), new LiteralByteString(new byte[]{}).toString(ByteString.UTF_8)); + ByteString.EMPTY.toString(Internal.UTF_8), new LiteralByteString(new byte[]{}).toString(Internal.UTF_8)); } public void testToString_raisesException() throws UnsupportedEncodingException{ diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java index 43872d1d23..cc38559927 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringSubstringTest.java @@ -116,7 +116,7 @@ public class RopeByteStringSubstringTest extends LiteralByteStringTest { assertEquals(classUnderTest + " from string must have the expected type", classUnderTest, getActualClassName(unicode)); - String roundTripString = unicode.toString(ByteString.UTF_8); + String roundTripString = unicode.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); diff --git a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java index e43df16525..bd0d15e341 100644 --- a/java/src/test/java/com/google/protobuf/RopeByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/RopeByteStringTest.java @@ -135,7 +135,7 @@ public class RopeByteStringTest extends LiteralByteStringTest { assertEquals(classUnderTest + " from string must have the expected type", classUnderTest, getActualClassName(unicode)); - String roundTripString = unicode.toString(ByteString.UTF_8); + String roundTripString = unicode.toString(Internal.UTF_8); assertEquals(classUnderTest + " unicode bytes must match", testString, roundTripString); ByteString flatString = ByteString.copyFromUtf8(testString); @@ -149,7 +149,7 @@ public class RopeByteStringTest extends LiteralByteStringTest { RopeByteString ropeByteString = RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); assertSame(classUnderTest + " must be the same string references", - ByteString.EMPTY.toString(ByteString.UTF_8), ropeByteString.toString(ByteString.UTF_8)); + ByteString.EMPTY.toString(Internal.UTF_8), ropeByteString.toString(Internal.UTF_8)); } public void testToString_raisesException() throws UnsupportedEncodingException{ diff --git a/java/src/test/java/com/google/protobuf/TestUtil.java b/java/src/test/java/com/google/protobuf/TestUtil.java index 0ce72b5ee4..19a96d0e47 100644 --- a/java/src/test/java/com/google/protobuf/TestUtil.java +++ b/java/src/test/java/com/google/protobuf/TestUtil.java @@ -276,7 +276,7 @@ public final class TestUtil { /** Helper to convert a String to ByteString. */ static ByteString toBytes(String str) { - return ByteString.copyFrom(str.getBytes(ByteString.UTF_8)); + return ByteString.copyFrom(str.getBytes(Internal.UTF_8)); } /** diff --git a/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java index 2f6bcd1a0c..e76b4a6752 100644 --- a/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java +++ b/java/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java @@ -229,7 +229,7 @@ public class UnknownFieldSetLiteTest extends TestCase { public void testMalformedBytes() throws Exception { try { - Foo.parseFrom("this is a malformed protocol buffer".getBytes(ByteString.UTF_8)); + Foo.parseFrom("this is a malformed protocol buffer".getBytes(Internal.UTF_8)); fail(); } catch (InvalidProtocolBufferException e) { // Expected. From 654bd3171f3f0d03bdc4d72edded4d6409398181 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 2 Apr 2015 14:47:16 -0700 Subject: [PATCH 7/7] Clean up imports --- .../google/protobuf/CodedOutputStream.java | 1 - .../java/com/google/protobuf/Descriptors.java | 1 - .../java/com/google/protobuf/Internal.java | 2 -- .../google/protobuf/LiteralByteString.java | 1 - .../com/google/protobuf/RopeByteString.java | 3 +-- .../com/google/protobuf/ByteStringTest.java | 2 +- .../com/google/protobuf/DescriptorsTest.java | 20 ++++++++----------- .../google/protobuf/IsValidUtf8TestUtil.java | 13 ++++++------ .../nano/CodedOutputByteBufferNano.java | 1 - .../google/protobuf/nano/InternalNano.java | 3 +-- 10 files changed, 17 insertions(+), 30 deletions(-) diff --git a/java/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/src/main/java/com/google/protobuf/CodedOutputStream.java index f5fd58f9af..954fde0861 100644 --- a/java/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -32,7 +32,6 @@ package com.google.protobuf; import java.io.IOException; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; /** diff --git a/java/src/main/java/com/google/protobuf/Descriptors.java b/java/src/main/java/com/google/protobuf/Descriptors.java index ab8930a1f8..b60bd36b01 100644 --- a/java/src/main/java/com/google/protobuf/Descriptors.java +++ b/java/src/main/java/com/google/protobuf/Descriptors.java @@ -43,7 +43,6 @@ import java.util.Map; import java.util.Set; import java.util.WeakHashMap; import java.util.logging.Logger; -import java.io.UnsupportedEncodingException; /** * Contains a collection of classes which describe protocol message types. diff --git a/java/src/main/java/com/google/protobuf/Internal.java b/java/src/main/java/com/google/protobuf/Internal.java index b49318ecee..74bf44c05b 100644 --- a/java/src/main/java/com/google/protobuf/Internal.java +++ b/java/src/main/java/com/google/protobuf/Internal.java @@ -30,8 +30,6 @@ 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; diff --git a/java/src/main/java/com/google/protobuf/LiteralByteString.java b/java/src/main/java/com/google/protobuf/LiteralByteString.java index 3462c3957b..c5a8512a06 100644 --- a/java/src/main/java/com/google/protobuf/LiteralByteString.java +++ b/java/src/main/java/com/google/protobuf/LiteralByteString.java @@ -34,7 +34,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; diff --git a/java/src/main/java/com/google/protobuf/RopeByteString.java b/java/src/main/java/com/google/protobuf/RopeByteString.java index 0900a04208..4f3eb127ac 100644 --- a/java/src/main/java/com/google/protobuf/RopeByteString.java +++ b/java/src/main/java/com/google/protobuf/RopeByteString.java @@ -30,13 +30,12 @@ package com.google.protobuf; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InvalidObjectException; import java.io.ObjectInputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.io.ByteArrayInputStream; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.ArrayList; diff --git a/java/src/test/java/com/google/protobuf/ByteStringTest.java b/java/src/test/java/com/google/protobuf/ByteStringTest.java index 46c229ad9f..8af5dcde31 100644 --- a/java/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/src/test/java/com/google/protobuf/ByteStringTest.java @@ -36,8 +36,8 @@ import junit.framework.TestCase; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; diff --git a/java/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/src/test/java/com/google/protobuf/DescriptorsTest.java index fee9ca0443..6cfa18d543 100644 --- a/java/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -35,32 +35,28 @@ import com.google.protobuf.DescriptorProtos.EnumDescriptorProto; import com.google.protobuf.DescriptorProtos.EnumValueDescriptorProto; import com.google.protobuf.DescriptorProtos.FieldDescriptorProto; import com.google.protobuf.DescriptorProtos.FileDescriptorProto; -import com.google.protobuf.Descriptors.DescriptorValidationException; -import com.google.protobuf.Descriptors.FileDescriptor; import com.google.protobuf.Descriptors.Descriptor; -import com.google.protobuf.Descriptors.FieldDescriptor; -import com.google.protobuf.Descriptors.OneofDescriptor; +import com.google.protobuf.Descriptors.DescriptorValidationException; import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.EnumValueDescriptor; -import com.google.protobuf.Descriptors.ServiceDescriptor; +import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.protobuf.Descriptors.FileDescriptor; import com.google.protobuf.Descriptors.MethodDescriptor; - +import com.google.protobuf.Descriptors.OneofDescriptor; +import com.google.protobuf.Descriptors.ServiceDescriptor; import com.google.protobuf.test.UnittestImport; import com.google.protobuf.test.UnittestImport.ImportEnum; -import com.google.protobuf.test.UnittestImport.ImportMessage; +import protobuf_unittest.TestCustomOptions; +import protobuf_unittest.UnittestCustomOptions; import protobuf_unittest.UnittestProto; import protobuf_unittest.UnittestProto.ForeignEnum; import protobuf_unittest.UnittestProto.ForeignMessage; -import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestAllExtensions; +import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; import protobuf_unittest.UnittestProto.TestMultipleExtensionRanges; import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestService; -import protobuf_unittest.UnittestCustomOptions; - -import protobuf_unittest.TestCustomOptions; - import junit.framework.TestCase; diff --git a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java index 320370396f..321669f314 100644 --- a/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java +++ b/java/src/test/java/com/google/protobuf/IsValidUtf8TestUtil.java @@ -33,18 +33,17 @@ package com.google.protobuf; import static junit.framework.Assert.*; import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; +import java.nio.charset.CodingErrorAction; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.logging.Logger; -import java.nio.charset.CharsetDecoder; -import java.nio.charset.Charset; -import java.nio.charset.CodingErrorAction; -import java.nio.charset.CharsetEncoder; -import java.nio.charset.CoderResult; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; /** * Shared testing code for {@link IsValidUtf8Test} and diff --git a/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java index 1f0534b173..3590718308 100644 --- a/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java +++ b/javanano/src/main/java/com/google/protobuf/nano/CodedOutputByteBufferNano.java @@ -31,7 +31,6 @@ package com.google.protobuf.nano; import java.io.IOException; -import java.io.UnsupportedEncodingException; /** * Encodes and writes protocol message fields. 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 67404d27b5..e7ba8d1262 100644 --- a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java +++ b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java @@ -33,11 +33,10 @@ package com.google.protobuf.nano; 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.Entry; import java.util.Map; +import java.util.Map.Entry; /** * The classes contained within are used internally by the Protocol Buffer