File tree Expand file tree Collapse file tree 3 files changed +35
-6
lines changed
main/java/org/elasticsearch/common/io/stream
test/java/org/elasticsearch/common/io/streams Expand file tree Collapse file tree 3 files changed +35
-6
lines changed Original file line number Diff line number Diff line change @@ -335,6 +335,21 @@ public String[] readStringArray() throws IOException {
335
335
return ret ;
336
336
}
337
337
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
+
338
353
@ Nullable
339
354
public Map <String , Object > readMap () throws IOException {
340
355
return (Map <String , Object >) readGenericValue ();
@@ -427,7 +442,7 @@ public int[] readIntArray() throws IOException {
427
442
}
428
443
return values ;
429
444
}
430
-
445
+
431
446
public long [] readLongArray () throws IOException {
432
447
int length = readVInt ();
433
448
long [] values = new long [length ];
@@ -436,7 +451,7 @@ public long[] readLongArray() throws IOException {
436
451
}
437
452
return values ;
438
453
}
439
-
454
+
440
455
public float [] readFloatArray () throws IOException {
441
456
int length = readVInt ();
442
457
float [] values = new float [length ];
@@ -445,7 +460,7 @@ public float[] readFloatArray() throws IOException {
445
460
}
446
461
return values ;
447
462
}
448
-
463
+
449
464
public double [] readDoubleArray () throws IOException {
450
465
int length = readVInt ();
451
466
double [] values = new double [length ];
Original file line number Diff line number Diff line change @@ -283,6 +283,16 @@ public void writeStringArray(String[] array) throws IOException {
283
283
}
284
284
}
285
285
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
+
286
296
/**
287
297
* Writes a string array, for nullable string, writes it as 0 (empty string).
288
298
*/
@@ -399,21 +409,21 @@ public void writeIntArray(int[] value) throws IOException {
399
409
writeInt (value [i ]);
400
410
}
401
411
}
402
-
412
+
403
413
public void writeLongArray (long [] value ) throws IOException {
404
414
writeVInt (value .length );
405
415
for (int i =0 ; i <value .length ; i ++) {
406
416
writeLong (value [i ]);
407
417
}
408
418
}
409
-
419
+
410
420
public void writeFloatArray (float [] value ) throws IOException {
411
421
writeVInt (value .length );
412
422
for (int i =0 ; i <value .length ; i ++) {
413
423
writeFloat (value [i ]);
414
424
}
415
425
}
416
-
426
+
417
427
public void writeDoubleArray (double [] value ) throws IOException {
418
428
writeVInt (value .length );
419
429
for (int i =0 ; i <value .length ; i ++) {
Original file line number Diff line number Diff line change 28
28
import org .junit .Ignore ;
29
29
import org .junit .Test ;
30
30
31
+ import java .util .Arrays ;
32
+
31
33
import static org .hamcrest .Matchers .closeTo ;
32
34
import static org .hamcrest .Matchers .equalTo ;
33
35
@@ -282,6 +284,7 @@ public void testSimpleStreams() throws Exception {
282
284
out .writeGenericValue (doubleArray );
283
285
out .writeString ("hello" );
284
286
out .writeString ("goodbye" );
287
+ out .writeStringList (Arrays .asList (new String []{"Hello" , "Again" }));
285
288
out .writeGenericValue (BytesRefs .toBytesRef ("bytesref" ));
286
289
BytesStreamInput in = new BytesStreamInput (out .bytes ().toBytes ());
287
290
assertThat (in .readBoolean (), equalTo (false ));
@@ -299,6 +302,7 @@ public void testSimpleStreams() throws Exception {
299
302
assertThat (in .readGenericValue (), equalTo ((Object )doubleArray ));
300
303
assertThat (in .readString (), equalTo ("hello" ));
301
304
assertThat (in .readString (), equalTo ("goodbye" ));
305
+ assertThat (in .readStringList (), equalTo (Arrays .asList (new String []{"Hello" , "Again" })));
302
306
assertThat (in .readGenericValue (), equalTo ((Object )BytesRefs .toBytesRef ("bytesref" )));
303
307
in .close ();
304
308
out .close ();
You can’t perform that action at this time.
0 commit comments