aboutsummaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorkenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2010-02-01 17:41:59 +0000
committerkenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d>2010-02-01 17:41:59 +0000
commitdaee05168eb0f2cf102b7ef80c0af87c80729612 (patch)
tree9315d4071b5a7c9dc8501efb8ddb1f1d0bf09b3b /java
parentab6950d75d6bae6a1574726ee1e7060e001ed23a (diff)
downloadprotobuf-daee05168eb0f2cf102b7ef80c0af87c80729612.tar.gz
protobuf-daee05168eb0f2cf102b7ef80c0af87c80729612.tar.bz2
protobuf-daee05168eb0f2cf102b7ef80c0af87c80729612.zip
Optimize Java string serialization. Patch from Evan Jones.
Diffstat (limited to 'java')
-rw-r--r--java/src/main/java/com/google/protobuf/CodedOutputStream.java17
-rw-r--r--java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java24
2 files changed, 41 insertions, 0 deletions
diff --git a/java/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/src/main/java/com/google/protobuf/CodedOutputStream.java
index 58dd1506..18da6d9d 100644
--- a/java/src/main/java/com/google/protobuf/CodedOutputStream.java
+++ b/java/src/main/java/com/google/protobuf/CodedOutputStream.java
@@ -193,6 +193,23 @@ public final class CodedOutputStream {
writeStringNoTag(value);
}
+ /**
+ * Write a {@code string} field, including tag, to the stream, where bytes
+ * is the encoded version of value. Used by the SPEED version of messages
+ * to avoid performing the UTF-8 conversion twice. bytes is simply a hint
+ * and may be null. If it is null, value will be converted as usual.
+ */
+ public void writeStringCached(final int fieldNumber, final String value,
+ ByteString bytes)
+ throws IOException {
+ // The cache can be null if serializing without getting the size first, or
+ // if there are multiple threads.
+ if (bytes == null) {
+ bytes = ByteString.copyFromUtf8(value);
+ }
+ writeBytes(fieldNumber, bytes);
+ }
+
/** Write a {@code group} field, including tag, to the stream. */
public void writeGroup(final int fieldNumber, final MessageLite value)
throws IOException {
diff --git a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java
index 48e54657..85691d60 100644
--- a/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java
+++ b/java/src/test/java/com/google/protobuf/CodedOutputStreamTest.java
@@ -36,6 +36,7 @@ import protobuf_unittest.UnittestProto.TestPackedTypes;
import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -211,6 +212,29 @@ public class CodedOutputStreamTest extends TestCase {
0x9abcdef012345678L);
}
+ /** Test writing cached strings. */
+ public void testWriteStringCached() throws IOException {
+ final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ final CodedOutputStream stream = CodedOutputStream.newInstance(output);
+
+ // Test writing a string that is not cached
+ stream.writeStringCached(5, "hello", null);
+ stream.flush();
+ CodedInputStream in = CodedInputStream.newInstance(output.toByteArray());
+ assertEquals(WireFormat.makeTag(5, WireFormat.WIRETYPE_LENGTH_DELIMITED),
+ in.readTag());
+ assertEquals("hello", in.readString());
+
+ // Write a cached string: the real string is ignored
+ output.reset();
+ stream.writeStringCached(5, "ignored", ByteString.copyFromUtf8("hello"));
+ stream.flush();
+ in = CodedInputStream.newInstance(output.toByteArray());
+ assertEquals(WireFormat.makeTag(5, WireFormat.WIRETYPE_LENGTH_DELIMITED),
+ in.readTag());
+ assertEquals("hello", in.readString());
+ }
+
/** Test encodeZigZag32() and encodeZigZag64(). */
public void testEncodeZigZag() throws Exception {
assertEquals(0, CodedOutputStream.encodeZigZag32( 0));