diff options
author | Dave Hawkey <dhawkey@google.com> | 2014-03-20 10:55:41 -0600 |
---|---|---|
committer | Dave Hawkey <dhawkey@google.com> | 2014-03-21 09:27:23 -0600 |
commit | c6e12c6702ca764486f952654ba1568f00efe813 (patch) | |
tree | b1dcfb28a8cef29d898e45637852816eb490ddc2 /java/src/main/java/com/google/protobuf/nano/MessageNano.java | |
parent | 51ef8f39de376fe71ce5d2c682abe4f974cf8074 (diff) | |
download | external_protobuf-c6e12c6702ca764486f952654ba1568f00efe813.zip external_protobuf-c6e12c6702ca764486f952654ba1568f00efe813.tar.gz external_protobuf-c6e12c6702ca764486f952654ba1568f00efe813.tar.bz2 |
Don't reset cachedSize to 0 in getSerializedSize
This avoids a race-condition when cachedSize is momentarily set to 0
for non-empty messages if multiple threads call getSerializedSize
(e.g. during serialization).
Change-Id: I15a8ded92edbf41bf1c8d787960c5bbbc8a323c5
Diffstat (limited to 'java/src/main/java/com/google/protobuf/nano/MessageNano.java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/nano/MessageNano.java | 19 |
1 files changed, 14 insertions, 5 deletions
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 82dc6cc..3119c93 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. @@ -60,10 +60,19 @@ public abstract class MessageNano { * The size is cached and the cached result can be retrieved * using getCachedSize(). */ - public int getSerializedSize() { - // This is overridden if the generated message has serialized fields. - cachedSize = 0; - return 0; + public final int getSerializedSize() { + 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; } /** |