InputStream data = realResponse.getEntity().getContent();
byte[] preview = new byte[100];
data.read(preview, 0, 100);
// Now I want to refer to the InputStream later on, but I want it from the beginning of the stream, not 100 bytes in. I tried mark()
it at 100, and then reset()
after I read the first 100 bytes, but that doesn't work either.
Any ideas? Probably a stupid mistake..just not seeing it.
If the
InputStream
supports mark (you can check with themarkSupported()
method), then the following should work:However, be aware that
data.read(preview, 0, 100)
is not guaranteed to read 100 bytes in one go, it may read less.When you use
mark()
of the java.io.InputStream object you should check with themarkSupported()
method if your InputStream actually support using mark. According to the API theInputStream
class doesn't, but the java.io.BufferedInputStream class does. Maybe you should embed your stream inside aBufferedInputStream
object like: