0

I learned from the question: What are mark and reset in BufferedReader? that the parameter of mark function will limit the number of chars can be read after the mark.

If the parameter is 0, doesn't it mean that any read after mark(0) should invalidate the mark? Why does reset() still works?

Again, I got an example of setting this parameter to a different number 50, and it reports IOException: Mark invalid. http://www.coderanch.com/t/426468/java-io/java/Mark-reset-inputstream

So I just wrote an example myself. However, the readlimite parameter doesn't seem to work anyway. Here is my program

 import java.io.BufferedInputStream;
 import java.io.FileInputStream;
 import java.io.FilterInputStream;
 import java.io.InputStream;
 public class TestMarkReset {
 public static void main(String[] args) throws Exception {
                // create input streams
        InputStream is = new FileInputStream("/home/peipei/test.txt");
        FilterInputStream fis = new BufferedInputStream(is);

        // reads and prints BufferedReader
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());

        // mark invoked at this position
        fis.mark(1);
        System.out.println("mark() invoked");
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());
        // reset() repositioned the stream to the mark
        fis.reset();
        System.out.println("reset() invoked");
        System.out.println((char) fis.read());
        System.out.println((char) fis.read());

      }

}

Can anyone explain me what's happening to this parameter? Why sometimes it works, sometimes it doesn't work?

Community
  • 1
  • 1
Peipei
  • 136
  • 1
  • 9

1 Answers1

0

You should say any read after mark(0) could invalidate the mark?

The only question is; is the point you marked still in the buffer? If it is you can still reset() to it. if not, you can't because it has been lost.

It would be very expensive for the buffer to be removing one byte at a time as you read it. It only clears the buffer when you need to read more data from the OS.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Hi Peter, let me write down what I understood from your answer. For example the default buffer size is 8K in java IO. So if you read just 10-20 bytes after marking, it doesn't matter because the point is still in the buffer. However, if the read size is larger, maybe 8197 bytes, the data of marked point is erased and filled with new data. That's when the mark becomes invalid. Is this understanding correct? – Peipei May 23 '14 at 16:35
  • @Peipei I believe that is how it is implemented in OpenJDK currently. Note: other implementations might be different as this behaviour is not specified. – Peter Lawrey May 23 '14 at 17:31
  • The why we are passing an integer argument to the method mark(). Is it not useless? – my name is GYAN Sep 29 '16 at 13:43
  • @mynameisGYAN the int value indicates how much it might need to buffer. – Peter Lawrey Sep 29 '16 at 13:47