aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
Diffstat (limited to 'java/src')
-rw-r--r--java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java3
-rw-r--r--java/src/main/java/com/google/protobuf/nano/MessageNano.java17
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java12
3 files changed, 26 insertions, 6 deletions
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 839f21c..63c8afc 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 82dc6cc..d6288c9 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 9987cac..5f28647 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;