diff options
Diffstat (limited to 'core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java')
-rw-r--r-- | core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 6b7607a3e7..55e45e8baa 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -146,6 +146,19 @@ public abstract class StreamOutput extends OutputStream { bytes.writeTo(this); } + /** + * Writes an optional bytes reference including a length header. Use this if you need to differentiate between null and empty bytes + * references. Use {@link #writeBytesReference(BytesReference)} and {@link StreamInput#readBytesReference()} if you do not. + */ + public void writeOptionalBytesReference(@Nullable BytesReference bytes) throws IOException { + if (bytes == null) { + writeVInt(0); + return; + } + writeVInt(bytes.length() + 1); + bytes.writeTo(this); + } + public void writeBytesRef(BytesRef bytes) throws IOException { if (bytes == null) { writeVInt(0); |