Forum Stats

  • 3,876,215 Users
  • 2,267,082 Discussions


What is the use of readAheadLimit parameter in the method mark of BufferedReader class?

I am new to Java language. As I was playing around with the BufferedReader class, I came across the parameter readAheadLimit in the mark method. though I read its definition, still I am not able to understand it. I searched around different websites to get its clear meaning . almost all the websites I visited quoted the definition present in the oracle document. I want some one to help me understand the definition of readAheadLimit with some examples.


  • The readAheadLimit parameter determines the number of characters that can be read from the stream while still preserving the mark. Put the mark and keep reading from the stream, observe the mark retained only up to the point of the read operations reach this readAheadLimit, after that the mark would no longer be valid.

  • User_LH8PA
    User_LH8PA Member Posts: 1 Green Ribbon
    public class SecondProgram {
        public static void main(String[] args)  {
           try {
                InputStreamReader is = new InputStreamReader(;
                BufferedReader br = new BufferedReader(is);
                System.out.println("Enter the input");
                System.out.println("MarkSupported "+br.markSupported());
                System.out.println("Mark ");
           }catch(Exception ex){

    I executed the program. Following lines I just copied from my terminal and they include both input and output.

    Enter the input

    MarkSupported true





    Now my question is I gave readAheadLimit as 5 .please check the line br.mark(5) in the program. the very next statement I read a line from the stream using readLine() method. as per the terminal input&output section ,I am trying to read characters size more than 5 which I set as a limit for readAhead (abcdefghijklmnopq).then when I try to reset, it should have invalidated the mark because I read more than the allowed limit in the previous line.but it is not happening. rather it reset the mark to the previous position and reads once again the same line and prints it. then this contradicts the definition of the readAheadLimit. Do you have any suggestion. I thank you for your previous comments @vasily.

  • Vasily Strelnikov-Oracle
    Vasily Strelnikov-Oracle Member Posts: 15 Employee
    edited Apr 28, 2021 9:26PM

    I think I understand you question now. The key is the word "may" in the java documentation: "An attempt to reset the stream after reading characters up to this limit or beyond may fail". So it may or may not allow the position to reset.

    import java.nio.charset.*;
    public class MarkTest {
      public static void main(String[] args) {
      /* there are only 10 chars per line */
      String text = "12345\n6789\n";
      try (BufferedReader in = new BufferedReader(
      new InputStreamReader(
      new ByteArrayInputStream(
      text.getBytes(Charset.forName("UTF-8")))))) {
      System.out.println("MarkSupported "+in.markSupported());
    /* position 2 char in the stream */
    /* set maker at current poistion, so that readAheadLimit allows to read up to 10 chars, before maker is forgotten */
    /* read from position a remainder of the line of text, which contains less then readAheadLimit ammount */
      System.out.println("Read from the marker: "+in.readLine());
    /* reset stream back to the marker position, which will be guranteed */
    /* read same text again */
      System.out.println("Read from the marker again: "+in.readLine());
    /* reset stream back to the marker position */
    /* set maker, so that readAheadLimit allows to read up to 2 chars, before maker is forgotten */
    /* read same text again */
      System.out.println("Read from the marker again: "+in.readLine());
    /* reset stream back to the marker position, which may or may not be the original position, because there is no gurantee that the marker will be preserved when codes reads data beyoud readAheadLimit */
    /* resetting stream back to the marker position may succeed, or fail, because we've read more chars then the readAheadLimit value */
      System.out.println("Read from the marker again, or from the start of the second line: "+in.readLine());
      }catch(Exception ex){