src/share/classes/java/io/BufferedReader.java

Print this page




  37  * read request to be made of the underlying character or byte stream.  It is
  38  * therefore advisable to wrap a BufferedReader around any Reader whose read()
  39  * operations may be costly, such as FileReaders and InputStreamReaders.  For
  40  * example,
  41  *
  42  * <pre>
  43  * BufferedReader in
  44  *   = new BufferedReader(new FileReader("foo.in"));
  45  * </pre>
  46  *
  47  * will buffer the input from the specified file.  Without buffering, each
  48  * invocation of read() or readLine() could cause bytes to be read from the
  49  * file, converted into characters, and then returned, which can be very
  50  * inefficient.
  51  *
  52  * <p> Programs that use DataInputStreams for textual input can be localized by
  53  * replacing each DataInputStream with an appropriate BufferedReader.
  54  *
  55  * @see FileReader
  56  * @see InputStreamReader

  57  *
  58  * @author      Mark Reinhold
  59  * @since       JDK1.1
  60  */
  61 
  62 public class BufferedReader extends Reader {
  63 
  64     private Reader in;
  65 
  66     private char cb[];
  67     private int nChars, nextChar;
  68 
  69     private static final int INVALIDATED = -2;
  70     private static final int UNMARKED = -1;
  71     private int markedChar = UNMARKED;
  72     private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
  73 
  74     /** If the next character is a line feed, skip it */
  75     private boolean skipLF = false;
  76 


 357                     return str;
 358                 }
 359 
 360                 if (s == null)
 361                     s = new StringBuffer(defaultExpectedLineLength);
 362                 s.append(cb, startChar, i - startChar);
 363             }
 364         }
 365     }
 366 
 367     /**
 368      * Reads a line of text.  A line is considered to be terminated by any one
 369      * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
 370      * followed immediately by a linefeed.
 371      *
 372      * @return     A String containing the contents of the line, not including
 373      *             any line-termination characters, or null if the end of the
 374      *             stream has been reached
 375      *
 376      * @exception  IOException  If an I/O error occurs


 377      */
 378     public String readLine() throws IOException {
 379         return readLine(false);
 380     }
 381 
 382     /**
 383      * Skips characters.
 384      *
 385      * @param  n  The number of characters to skip
 386      *
 387      * @return    The number of characters actually skipped
 388      *
 389      * @exception  IllegalArgumentException  If <code>n</code> is negative.
 390      * @exception  IOException  If an I/O error occurs
 391      */
 392     public long skip(long n) throws IOException {
 393         if (n < 0L) {
 394             throw new IllegalArgumentException("skip value is negative");
 395         }
 396         synchronized (lock) {




  37  * read request to be made of the underlying character or byte stream.  It is
  38  * therefore advisable to wrap a BufferedReader around any Reader whose read()
  39  * operations may be costly, such as FileReaders and InputStreamReaders.  For
  40  * example,
  41  *
  42  * <pre>
  43  * BufferedReader in
  44  *   = new BufferedReader(new FileReader("foo.in"));
  45  * </pre>
  46  *
  47  * will buffer the input from the specified file.  Without buffering, each
  48  * invocation of read() or readLine() could cause bytes to be read from the
  49  * file, converted into characters, and then returned, which can be very
  50  * inefficient.
  51  *
  52  * <p> Programs that use DataInputStreams for textual input can be localized by
  53  * replacing each DataInputStream with an appropriate BufferedReader.
  54  *
  55  * @see FileReader
  56  * @see InputStreamReader
  57  * @see java.nio.file.Files#newBufferedReader
  58  *
  59  * @author      Mark Reinhold
  60  * @since       JDK1.1
  61  */
  62 
  63 public class BufferedReader extends Reader {
  64 
  65     private Reader in;
  66 
  67     private char cb[];
  68     private int nChars, nextChar;
  69 
  70     private static final int INVALIDATED = -2;
  71     private static final int UNMARKED = -1;
  72     private int markedChar = UNMARKED;
  73     private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
  74 
  75     /** If the next character is a line feed, skip it */
  76     private boolean skipLF = false;
  77 


 358                     return str;
 359                 }
 360 
 361                 if (s == null)
 362                     s = new StringBuffer(defaultExpectedLineLength);
 363                 s.append(cb, startChar, i - startChar);
 364             }
 365         }
 366     }
 367 
 368     /**
 369      * Reads a line of text.  A line is considered to be terminated by any one
 370      * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
 371      * followed immediately by a linefeed.
 372      *
 373      * @return     A String containing the contents of the line, not including
 374      *             any line-termination characters, or null if the end of the
 375      *             stream has been reached
 376      *
 377      * @exception  IOException  If an I/O error occurs
 378      *
 379      * @see java.nio.file.Files#readAllLines
 380      */
 381     public String readLine() throws IOException {
 382         return readLine(false);
 383     }
 384 
 385     /**
 386      * Skips characters.
 387      *
 388      * @param  n  The number of characters to skip
 389      *
 390      * @return    The number of characters actually skipped
 391      *
 392      * @exception  IllegalArgumentException  If <code>n</code> is negative.
 393      * @exception  IOException  If an I/O error occurs
 394      */
 395     public long skip(long n) throws IOException {
 396         if (n < 0L) {
 397             throw new IllegalArgumentException("skip value is negative");
 398         }
 399         synchronized (lock) {