Search code examples
javaresetbufferedreader

Mark()/Reset BufferedReader in java


I thoroughly understand how the mechanism mark and reset works: However I would like to know why the following code taken from here, works even if I replace br.mark(26) with br.mark(0). Since the parameter is the mumber of char to be read ahead of the mark. If I put 0 as argument it should not read anything. instead it works as if I did not change anything. Why does it behave in that way?

br = new BufferedReader(isr);
     // reads and prints BufferedReader
     System.out.println((char)br.read());
     System.out.println((char)br.read());
     System.out.println((char)br.read());
     // mark invoked at this position
     br.mark(0);


     System.out.println("mark() invoked");
     System.out.println((char)br.read());
     System.out.println((char)br.read());
     System.err.println("The Thing to be said:"+(char)br.read());

     // reset() repositioned the stream to the mark
     br.reset();
     System.out.println("reset() invoked");
     System.out.println((char)br.read());
     System.out.println((char)br.read());
     System.out.println((char)br.read());

Solution

  • Because it's not a strict limit, it's just advice. as the javadoc states:

    An attempt to reset the stream after reading characters up to this limit or beyond may fail.

    (emphasis mine)