diff options
Diffstat (limited to 'java/src/main/java/com/google/protobuf/AbstractMessage.java')
-rw-r--r-- | java/src/main/java/com/google/protobuf/AbstractMessage.java | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/java/src/main/java/com/google/protobuf/AbstractMessage.java b/java/src/main/java/com/google/protobuf/AbstractMessage.java index 24c32a47..53175cdd 100644 --- a/java/src/main/java/com/google/protobuf/AbstractMessage.java +++ b/java/src/main/java/com/google/protobuf/AbstractMessage.java @@ -32,6 +32,7 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.FieldDescriptor; +import java.io.FilterInputStream; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; @@ -152,6 +153,13 @@ public abstract class AbstractMessage implements Message { codedOutput.flush(); } + public void writeDelimitedTo(OutputStream output) throws IOException { + CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + codedOutput.writeRawVarint32(getSerializedSize()); + writeTo(codedOutput); + codedOutput.flush(); + } + private int memoizedSize = -1; public int getSerializedSize() { @@ -207,7 +215,8 @@ public abstract class AbstractMessage implements Message { if (getDescriptorForType() != otherMessage.getDescriptorForType()) { return false; } - return getAllFields().equals(otherMessage.getAllFields()); + return getAllFields().equals(otherMessage.getAllFields()) && + getUnknownFields().equals(otherMessage.getUnknownFields()); } @Override @@ -215,6 +224,7 @@ public abstract class AbstractMessage implements Message { int hash = 41; hash = (19 * hash) + getDescriptorForType().hashCode(); hash = (53 * hash) + getAllFields().hashCode(); + hash = (29 * hash) + getUnknownFields().hashCode(); return hash; } @@ -397,5 +407,51 @@ public abstract class AbstractMessage implements Message { codedInput.checkLastTagWas(0); return (BuilderType) this; } + + public BuilderType mergeDelimitedFrom(InputStream input, + ExtensionRegistry extensionRegistry) + throws IOException { + final int size = CodedInputStream.readRawVarint32(input); + + // A stream which will not read more than |size| bytes. + InputStream limitedInput = new FilterInputStream(input) { + int limit = size; + + @Override + public int available() throws IOException { + return Math.min(super.available(), limit); + } + + @Override + public int read() throws IOException { + if (limit <= 0) return -1; + int result = super.read(); + if (result >= 0) --limit; + return result; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (limit <= 0) return -1; + len = Math.min(len, limit); + int result = super.read(b, off, len); + if (result >= 0) limit -= result; + return result; + } + + @Override + public long skip(long n) throws IOException { + long result = super.skip(Math.min(n, limit)); + if (result >= 0) limit -= result; + return result; + } + }; + return mergeFrom(limitedInput, extensionRegistry); + } + + public BuilderType mergeDelimitedFrom(InputStream input) + throws IOException { + return mergeDelimitedFrom(input, ExtensionRegistry.getEmptyRegistry()); + } } } |