add read/write optional text

This commit is contained in:
Shay Banon 2013-01-07 02:54:22 +01:00
parent 0e5287f1f2
commit bcdda811ef
2 changed files with 17 additions and 0 deletions

View File

@ -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();

View File

@ -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();