From 6f4c13e5e7454b7544cf96c406664c1969b9f119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 8 May 2015 12:44:00 +0200 Subject: [PATCH] Transport: read/write support for list of strings Add support for reading and writng string lists to existing StreamInput and StreamOutput class. --- .../common/io/stream/StreamInput.java | 21 ++++++++++++++++--- .../common/io/stream/StreamOutput.java | 16 +++++++++++--- .../common/io/streams/BytesStreamsTests.java | 4 ++++ 3 files changed, 35 insertions(+), 6 deletions(-) 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 c529bc850ff95..5a19b12be0a13 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -335,6 +335,21 @@ public String[] readStringArray() throws IOException { return ret; } + /** + * Read in a list of strings. List can be empty but not {@code null}. + */ + public List readStringList() throws IOException { + int size = readVInt(); + if (size == 0) { + return Collections.emptyList(); + } + List ret = new ArrayList<>(size); + for (int i = 0; i < size; i++) { + ret.add(readString()); + } + return ret; + } + @Nullable public Map readMap() throws IOException { return (Map) readGenericValue(); @@ -427,7 +442,7 @@ public int[] readIntArray() throws IOException { } return values; } - + public long[] readLongArray() throws IOException { int length = readVInt(); long[] values = new long[length]; @@ -436,7 +451,7 @@ public long[] readLongArray() throws IOException { } return values; } - + public float[] readFloatArray() throws IOException { int length = readVInt(); float[] values = new float[length]; @@ -445,7 +460,7 @@ public float[] readFloatArray() throws IOException { } return values; } - + public double[] readDoubleArray() throws IOException { int length = readVInt(); double[] values = new double[length]; 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 ad9af359fadf1..b77419073e0c6 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -283,6 +283,16 @@ public void writeStringArray(String[] array) throws IOException { } } + /** + * Write a list of strings. List can be empty but not {@code null}. + */ + public void writeStringList(List stringList) throws IOException { + writeVInt(stringList.size()); + for (String s : stringList) { + writeString(s); + } + } + /** * Writes a string array, for nullable string, writes it as 0 (empty string). */ @@ -399,21 +409,21 @@ public void writeIntArray(int[] value) throws IOException { writeInt(value[i]); } } - + public void writeLongArray(long[] value) throws IOException { writeVInt(value.length); for (int i=0; i