diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index e3ed22c4bb6..79133d190a1 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -192,6 +192,15 @@ public abstract class StreamInput extends InputStream { return null; } + @Nullable + public Text readOptionalText() throws IOException { + int length = readInt(); + if (length == -1) { + return null; + } + return new StringAndBytesText(readBytesReference(length)); + } + public Text readText() throws IOException { // use StringAndBytes so we can cache the string if its ever converted to it int length = readInt(); diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 283576120b8..d9b495ad38f 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -184,6 +184,14 @@ public abstract class StreamOutput extends OutputStream { } } + public void writeOptionalText(@Nullable Text text) throws IOException { + if (text == null) { + writeInt(-1); + } else { + writeText(text); + } + } + public void writeText(Text text) throws IOException { if (!text.hasBytes() && seekPositionSupported()) { long pos1 = position();