Forums Register Login

About BufferedInputStream set method

+Pie Number of slices to send: Send
Hi everyone,

I have a BufferedInputStream in my program and I want to use the whole stream multiple times, so I wrote my code like this:

BufferedInputStream bis = new BufferedInputStream(inputStream);
if (bis.markSupported()) {
bis.mark(bis.available());
}

However when I called bis.mark() afterwards, there was a java.io.IOException: Resetting to invalid mark which indicate that the mark had became invalid.

May I know why the Exception happened since I've already assigned the whole size of the stream to the readlimit parameter of the mark method?

Thanks!

Kit
+Pie Number of slices to send: Send
Welcome to the Ranch!

You didn't mark the entire stream, just what was available at the time. There are absolutely no guarantees about what available() will return. See also Available Doesn't Do What You Think It Does.

Perhaps you should copy the contents of the input stream to a ByteArrayOutputStream, then use a ByteArrayInputStream. Because you will have copied the entire contents you know the size of the contents. This isn't even necessary however, as a ByteArrayInputStream by default puts a mark at the start / offset, and resetting will go back to this mark no matter how much data you requested to be read.
+Pie Number of slices to send: Send
 

Rob Spoor wrote:Welcome to the Ranch!

You didn't mark the entire stream, just what was available at the time. There are absolutely no guarantees about what available() will return. See also Available Doesn't Do What You Think It Does.

Perhaps you should copy the contents of the input stream to a ByteArrayOutputStream, then use a ByteArrayInputStream. Because you will have copied the entire contents you know the size of the contents. This isn't even necessary however, as a ByteArrayInputStream by default puts a mark at the start / offset, and resetting will go back to this mark no matter how much data you requested to be read.



Hi Rob, thanks for your reply

Actually I also wrote another program which is exactly like your suggested approach(lets say approach 2). Since I have to use the whole BufferedInputStream for three times, for approach 2 that means I have to create three ByteArrayInputStream objects as output which requires more memory, whereas the original approach only reads data from a single stream object.

If my original approach really not feasiable, I will just proceed with the suggested approach.

P.S. I find this forum(or ranch) very useful and I will come here more often!
1
+Pie Number of slices to send: Send
You don't need three ByteArrayInputStream objects. Just use its reset() method without calling mark, that will reset it to the starting position.

And even if you create three ByteArrayInputStream objects, just use the same byte[]. The three instances will then share the same data, and the only extra memory is needed for three int instance fields of ByteArrayInputStream, one reference field (to the byte[]) and the object overhead. That's not a lot.
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2966 times.
Similar Threads
RE:String handling in java
Download File
Input Output Questions
mark() in IO
BufferedInputStream mark() method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:35:38.