Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffered Input Stream mark read limit

I am learning how to use an InputStream. I was trying to use mark for BufferedInputStream, but when I try to reset I have these exceptions:

java.io.IOException: Resetting to invalid mark

I think this means that my mark read limit is set wrong. I actually don't know how to set the read limit in mark(). I tried like this:

is = new BufferedInputStream(is);
is.mark(is.available());

This is also wrong.

is.mark(16);

This also throws the same exception. How do I know what read limit I am supposed to set? Since I will be reading different file sizes from the input stream.

like image 871
Nur Aini Avatar asked Dec 22 '11 03:12

Nur Aini


People also ask

What is the optimal size of buffer in BufferedInputStream?

Optimal Buffer Size for a BufferedInputStream If the hard disk is anyways reading a minimum of 4KB at a time, it's stupid to use less than a 4KB buffer. It is also better to then use a buffer size that is a multiple of 4KB.

Which method is used to ensure the availability of input buffered stream to read data input?

read() method of BufferedInputStream class in Java is used to read the next byte of data from the input stream. When this read() method is called on the input stream then this read() method reads one character of the input stream at a time.

What is a buffered input stream?

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created.

What is buffered input output stream in Java?

BufferedOutputStream(OutputStream out) : Creates a new buffered output stream to write data to the specified underlying output stream. BufferedOutputStream(OutputStream out, int size) : Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.


2 Answers

This will read 5 times from the same BufferedInputStream.

for (int i=0; i<5; i++) {
   inputStream.mark(inputStream.available()+1);
   // Read from input stream
   Thumbnails.of(inputStream).forceSize(160, 160).toOutputStream(out);
   inputStream.reset();
}
like image 179
Ivan Kaplin Avatar answered Sep 24 '22 20:09

Ivan Kaplin


mark is sometimes useful if you need to inspect a few bytes beyond what you've read to decide what to do next, then you reset back to the mark and call the routine that expects the file pointer to be at the beginning of that logical part of the input. I don't think it is really intended for much else.

If you look at the javadoc for BufferedInputStream it says

The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

The key thing to remember here is once you mark a spot in the stream, if you keep reading beyond the marked length, the mark will no longer be valid, and the call to reset will fail. So mark is good for specific situations and not much use in other cases.

like image 34
Bill Avatar answered Sep 22 '22 20:09

Bill