InputStream will not reset to beginning

后端 未结 2 1494
情话喂你
情话喂你 2020-12-16 16:27
InputStream data = realResponse.getEntity().getContent();
byte[] preview = new byte[100];
data.read(preview, 0, 100);

// Now I want to refer to the

2条回答
  •  渐次进展
    2020-12-16 16:49

    If the InputStream supports mark (you can check with the markSupported() method), then the following should work:

    InputStream data = realResponse.getEntity().getContent();
    byte[] preview = new byte[100];
    data.mark(100);
    data.read(preview, 0, 100);
    data.reset();
    

    However, be aware that data.read(preview, 0, 100) is not guaranteed to read 100 bytes in one go, it may read less.

提交回复
热议问题