diff options
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java')
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java index 37d64633..b3fdebcb 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java @@ -786,6 +786,23 @@ public final class UnknownFieldSet implements MessageLite { } /** + * Serializes the message to a {@code ByteString} and returns it. This is just a trivial wrapper + * around {@link #writeTo(int, CodedOutputStream)}. + */ + public ByteString toByteString(int fieldNumber) { + try { + // TODO(lukes): consider caching serialized size in a volatile long + final ByteString.CodedBuilder out = + ByteString.newCodedBuilder(getSerializedSize(fieldNumber)); + writeTo(fieldNumber, out.getCodedOutput()); + return out.build(); + } catch (IOException e) { + throw new RuntimeException( + "Serializing to a ByteString should never fail with an IOException", e); + } + } + + /** * Serializes the field, including field number, and writes it to * {@code output}. */ |