diff options
author | Max Cai <maxtroy@google.com> | 2014-04-14 14:57:53 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-04-14 14:57:54 +0000 |
commit | 56cba8eeae57043aa507c13b190ab16015872135 (patch) | |
tree | 1501b441be329d4d550cf2601f1a8c65537d3b43 /java | |
parent | dc2ab875690f8061ba1a712872133120792e4746 (diff) | |
parent | 598087ef53c22ae3fd22f87d20d00bea2c16e949 (diff) | |
download | protobuf-56cba8eeae57043aa507c13b190ab16015872135.tar.gz protobuf-56cba8eeae57043aa507c13b190ab16015872135.tar.bz2 protobuf-56cba8eeae57043aa507c13b190ab16015872135.zip |
Merge "Don't reset cachedSize to 0 in getSerializedSize"
Diffstat (limited to 'java')
4 files changed, 35 insertions, 6 deletions
diff --git a/java/README.txt b/java/README.txt index 13865f6d..f958d147 100644 --- a/java/README.txt +++ b/java/README.txt @@ -437,6 +437,15 @@ and the runtime overhead. An overview of Nano features: MessageNano. - The 'bytes' type translates to the Java type byte[]. +The generated messages are not thread-safe for writes, but may be +used simultaneously from multiple threads in a read-only manner. +In other words, an appropriate synchronization mechanism (such as +a ReadWriteLock) must be used to ensure that a message, its +ancestors, and descendants are not accessed by any other threads +while the message is being modified. Field reads, getter methods, +toByteArray(...), writeTo(...), getCachedSize(), and +getSerializedSize() are all considered read-only operations. + IMPORTANT: If you have fields with defaults and opt out of accessors How fields with defaults are serialized has changed. Because we don't diff --git a/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java b/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java index 839f21c9..63c8afcb 100644 --- a/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java +++ b/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java @@ -47,7 +47,7 @@ public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>> protected List<UnknownFieldData> unknownFieldData; @Override - public int getSerializedSize() { + protected int computeSerializedSize() { int size = 0; int unknownFieldCount = unknownFieldData == null ? 0 : unknownFieldData.size(); for (int i = 0; i < unknownFieldCount; i++) { @@ -55,7 +55,6 @@ public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>> size += CodedOutputByteBufferNano.computeRawVarint32Size(unknownField.tag); size += unknownField.bytes.length; } - cachedSize = size; return size; } diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNano.java b/java/src/main/java/com/google/protobuf/nano/MessageNano.java index 82dc6cca..d6288c92 100644 --- a/java/src/main/java/com/google/protobuf/nano/MessageNano.java +++ b/java/src/main/java/com/google/protobuf/nano/MessageNano.java @@ -38,7 +38,7 @@ import java.io.IOException; * @author wink@google.com Wink Saville */ public abstract class MessageNano { - protected int cachedSize = -1; + protected volatile int cachedSize = -1; /** * Get the number of bytes required to encode this message. @@ -61,9 +61,18 @@ public abstract class MessageNano { * using getCachedSize(). */ public int getSerializedSize() { - // This is overridden if the generated message has serialized fields. - cachedSize = 0; - return 0; + int size = computeSerializedSize(); + cachedSize = size; + return size; + } + + /** + * Computes the number of bytes required to encode this message. This does not update the + * cached size. + */ + protected int computeSerializedSize() { + // This is overridden if the generated message has serialized fields. + return 0; } /** diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 9987cace..5f28647d 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -105,6 +105,12 @@ public class NanoTest extends TestCase { assertEquals(456, newMsg.d); assertEquals(2, msg.nestedMsg.bb); assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum); + + msg.nestedMsg = null; + assertTrue(msgSerializedSize != msg.getSerializedSize()); + + msg.clear(); + assertEquals(0, msg.getSerializedSize()); } public void testRecursiveMessageNano() throws Exception { @@ -143,6 +149,12 @@ public class NanoTest extends TestCase { assertEquals(3, newMsg.repeatedRecursiveMessageNano[0].id); } + public void testMessageNoFields() { + SingleMessageNano msg = new SingleMessageNano(); + assertEquals(0, msg.getSerializedSize()); + assertEquals(0, MessageNano.toByteArray(msg).length); + } + public void testNanoRequiredInt32() throws Exception { TestAllTypesNano msg = new TestAllTypesNano(); msg.id = 123; |