Skip to content

Commit 2a84818

Browse files
committed
Merge branch 'master' into feature/query-refactoring
2 parents 7c58f4a + 4215017 commit 2a84818

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/main/java/org/elasticsearch/common/io/stream/StreamInput.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,21 @@ public String[] readStringArray() throws IOException {
335335
return ret;
336336
}
337337

338+
/**
339+
* Read in a list of strings. List can be empty but not {@code null}.
340+
*/
341+
public List<String> readStringList() throws IOException {
342+
int size = readVInt();
343+
if (size == 0) {
344+
return Collections.emptyList();
345+
}
346+
List<String> ret = new ArrayList<>(size);
347+
for (int i = 0; i < size; i++) {
348+
ret.add(readString());
349+
}
350+
return ret;
351+
}
352+
338353
@Nullable
339354
public Map<String, Object> readMap() throws IOException {
340355
return (Map<String, Object>) readGenericValue();
@@ -427,7 +442,7 @@ public int[] readIntArray() throws IOException {
427442
}
428443
return values;
429444
}
430-
445+
431446
public long[] readLongArray() throws IOException {
432447
int length = readVInt();
433448
long[] values = new long[length];
@@ -436,7 +451,7 @@ public long[] readLongArray() throws IOException {
436451
}
437452
return values;
438453
}
439-
454+
440455
public float[] readFloatArray() throws IOException {
441456
int length = readVInt();
442457
float[] values = new float[length];
@@ -445,7 +460,7 @@ public float[] readFloatArray() throws IOException {
445460
}
446461
return values;
447462
}
448-
463+
449464
public double[] readDoubleArray() throws IOException {
450465
int length = readVInt();
451466
double[] values = new double[length];

src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ public void writeStringArray(String[] array) throws IOException {
283283
}
284284
}
285285

286+
/**
287+
* Write a list of strings. List can be empty but not {@code null}.
288+
*/
289+
public void writeStringList(List<String> stringList) throws IOException {
290+
writeVInt(stringList.size());
291+
for (String s : stringList) {
292+
writeString(s);
293+
}
294+
}
295+
286296
/**
287297
* Writes a string array, for nullable string, writes it as 0 (empty string).
288298
*/
@@ -399,21 +409,21 @@ public void writeIntArray(int[] value) throws IOException {
399409
writeInt(value[i]);
400410
}
401411
}
402-
412+
403413
public void writeLongArray(long[] value) throws IOException {
404414
writeVInt(value.length);
405415
for (int i=0; i<value.length; i++) {
406416
writeLong(value[i]);
407417
}
408418
}
409-
419+
410420
public void writeFloatArray(float[] value) throws IOException {
411421
writeVInt(value.length);
412422
for (int i=0; i<value.length; i++) {
413423
writeFloat(value[i]);
414424
}
415425
}
416-
426+
417427
public void writeDoubleArray(double[] value) throws IOException {
418428
writeVInt(value.length);
419429
for (int i=0; i<value.length; i++) {

src/test/java/org/elasticsearch/common/io/streams/BytesStreamsTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.junit.Ignore;
2929
import org.junit.Test;
3030

31+
import java.util.Arrays;
32+
3133
import static org.hamcrest.Matchers.closeTo;
3234
import static org.hamcrest.Matchers.equalTo;
3335

@@ -282,6 +284,7 @@ public void testSimpleStreams() throws Exception {
282284
out.writeGenericValue(doubleArray);
283285
out.writeString("hello");
284286
out.writeString("goodbye");
287+
out.writeStringList(Arrays.asList(new String[]{"Hello", "Again"}));
285288
out.writeGenericValue(BytesRefs.toBytesRef("bytesref"));
286289
BytesStreamInput in = new BytesStreamInput(out.bytes().toBytes());
287290
assertThat(in.readBoolean(), equalTo(false));
@@ -299,6 +302,7 @@ public void testSimpleStreams() throws Exception {
299302
assertThat(in.readGenericValue(), equalTo((Object)doubleArray));
300303
assertThat(in.readString(), equalTo("hello"));
301304
assertThat(in.readString(), equalTo("goodbye"));
305+
assertThat(in.readStringList(), equalTo(Arrays.asList(new String[]{"Hello", "Again"})));
302306
assertThat(in.readGenericValue(), equalTo((Object)BytesRefs.toBytesRef("bytesref")));
303307
in.close();
304308
out.close();

0 commit comments

Comments
 (0)