If you are using an implementation of InputStream
, you can check the result of InputStream#markSupported()
that tell you whether or not you can use the method mark()
/ reset()
.
If you can mark the stream when you read, then call reset()
to go back to begin.
If you can't you'll have to open a stream again.
Another solution would be to convert InputStream to byte array, then iterate over the array as many time as you need. You can find several solutions in this post Convert InputStream to byte array in Java using 3rd party libs or not. Caution, if the read content is too big you might experience some memory troubles.
Finally, if your need is to read image, then use :
BufferedImage image = ImageIO.read(new URL("http://www.example.com/images/toto.jpg"));
Using ImageIO#read(java.net.URL)
also allows you to use cache.