public void testStreamInput() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput si = pbr.streamInput();
    assertNotNull(si);

    // read single bytes one by one
    assertEquals(pbr.get(0), si.readByte());
    assertEquals(pbr.get(1), si.readByte());
    assertEquals(pbr.get(2), si.readByte());

    // reset the stream for bulk reading
    si.reset();

    // buffer for bulk reads
    byte[] origBuf = new byte[length];
    random().nextBytes(origBuf);
    byte[] targetBuf = Arrays.copyOf(origBuf, origBuf.length);

    // bulk-read 0 bytes: must not modify buffer
    si.readBytes(targetBuf, 0, 0);
    assertEquals(origBuf[0], targetBuf[0]);
    si.reset();

    // read a few few bytes as ints
    int bytesToRead = randomIntBetween(1, length / 2);
    for (int i = 0; i < bytesToRead; i++) {
      int b = si.read();
      assertEquals(pbr.get(i) & 0xff, b);
    }
    si.reset();

    // bulk-read all
    si.readFully(targetBuf);
    assertArrayEquals(pbr.toBytes(), targetBuf);

    // continuing to read should now fail with EOFException
    try {
      si.readByte();
      fail("expected EOF");
    } catch (EOFException | IndexOutOfBoundsException eof) {
      // yay
    }

    // try to read more than the stream contains
    si.reset();
    expectThrows(IndexOutOfBoundsException.class, () -> si.readBytes(targetBuf, 0, length * 2));
  }
예제 #2
0
 public BytesRef readBytesRef(int length) throws IOException {
   if (length == 0) {
     return new BytesRef();
   }
   byte[] bytes = new byte[length];
   readBytes(bytes, 0, length);
   return new BytesRef(bytes, 0, length);
 }
예제 #3
0
 /**
  * Reads a bytes reference from this stream, might hold an actual reference to the underlying
  * bytes of the stream.
  */
 public BytesReference readBytesReference(int length) throws IOException {
   if (length == 0) {
     return BytesArray.EMPTY;
   }
   byte[] bytes = new byte[length];
   readBytes(bytes, 0, length);
   return new BytesArray(bytes, 0, length);
 }
예제 #4
0
 public void readFully(byte[] b) throws IOException {
   readBytes(b, 0, b.length);
 }
예제 #5
0
 @SuppressWarnings({"unchecked"})
 @Nullable
 public Object readGenericValue() throws IOException {
   byte type = readByte();
   switch (type) {
     case -1:
       return null;
     case 0:
       return readString();
     case 1:
       return readInt();
     case 2:
       return readLong();
     case 3:
       return readFloat();
     case 4:
       return readDouble();
     case 5:
       return readBoolean();
     case 6:
       int bytesSize = readVInt();
       byte[] value = new byte[bytesSize];
       readBytes(value, 0, bytesSize);
       return value;
     case 7:
       int size = readVInt();
       List list = new ArrayList(size);
       for (int i = 0; i < size; i++) {
         list.add(readGenericValue());
       }
       return list;
     case 8:
       int size8 = readVInt();
       Object[] list8 = new Object[size8];
       for (int i = 0; i < size8; i++) {
         list8[i] = readGenericValue();
       }
       return list8;
     case 9:
       int size9 = readVInt();
       Map map9 = new LinkedHashMap(size9);
       for (int i = 0; i < size9; i++) {
         map9.put(readString(), readGenericValue());
       }
       return map9;
     case 10:
       int size10 = readVInt();
       Map map10 = new HashMap(size10);
       for (int i = 0; i < size10; i++) {
         map10.put(readString(), readGenericValue());
       }
       return map10;
     case 11:
       return readByte();
     case 12:
       return new Date(readLong());
     case 13:
       return new DateTime(readLong());
     case 14:
       return readBytesReference();
     case 15:
       return readText();
     case 16:
       return readShort();
     default:
       throw new IOException("Can't read unknown type [" + type + "]");
   }
 }