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?