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

Print this page
rev 7006 : 8003258: BufferedReader.lines()
Reviewed-by:
   1 /*
   2  * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 







  29 /**
  30  * Reads text from a character-input stream, buffering characters so as to
  31  * provide for the efficient reading of characters, arrays, and lines.
  32  *
  33  * <p> The buffer size may be specified, or the default size may be used.  The
  34  * default is large enough for most purposes.
  35  *
  36  * <p> In general, each read request made of a Reader causes a corresponding
  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


 504             if (markedChar < 0)
 505                 throw new IOException((markedChar == INVALIDATED)
 506                                       ? "Mark invalid"
 507                                       : "Stream not marked");
 508             nextChar = markedChar;
 509             skipLF = markedSkipLF;
 510         }
 511     }
 512 
 513     public void close() throws IOException {
 514         synchronized (lock) {
 515             if (in == null)
 516                 return;
 517             try {
 518                 in.close();
 519             } finally {
 520                 in = null;
 521                 cb = null;
 522             }
 523         }

























































 524     }
 525 }
   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 
  29 import java.util.Iterator;
  30 import java.util.NoSuchElementException;
  31 import java.util.Spliterator;
  32 import java.util.Spliterators;
  33 import java.util.stream.Stream;
  34 import java.util.stream.StreamSupport;
  35 
  36 /**
  37  * Reads text from a character-input stream, buffering characters so as to
  38  * provide for the efficient reading of characters, arrays, and lines.
  39  *
  40  * <p> The buffer size may be specified, or the default size may be used.  The
  41  * default is large enough for most purposes.
  42  *
  43  * <p> In general, each read request made of a Reader causes a corresponding
  44  * read request to be made of the underlying character or byte stream.  It is
  45  * therefore advisable to wrap a BufferedReader around any Reader whose read()
  46  * operations may be costly, such as FileReaders and InputStreamReaders.  For
  47  * example,
  48  *
  49  * <pre>
  50  * BufferedReader in
  51  *   = new BufferedReader(new FileReader("foo.in"));
  52  * </pre>
  53  *
  54  * will buffer the input from the specified file.  Without buffering, each
  55  * invocation of read() or readLine() could cause bytes to be read from the


 511             if (markedChar < 0)
 512                 throw new IOException((markedChar == INVALIDATED)
 513                                       ? "Mark invalid"
 514                                       : "Stream not marked");
 515             nextChar = markedChar;
 516             skipLF = markedSkipLF;
 517         }
 518     }
 519 
 520     public void close() throws IOException {
 521         synchronized (lock) {
 522             if (in == null)
 523                 return;
 524             try {
 525                 in.close();
 526             } finally {
 527                 in = null;
 528                 cb = null;
 529             }
 530         }
 531     }
 532 
 533     /**
 534      * Returns a {@code Stream}, the elements of which are lines read from this
 535      * {@code BufferedReader}.  The {@link Stream} is lazily populated via
 536      * calls to {@link #readLine()}.
 537      *
 538      * <p>Each element consumed by the {@code Stream} caused a line to be
 539      * read from this {@code BufferedReader}. Since the {@code Stream} does
 540      * not necessary consume all lines, it is possible to mix and use
 541      * different read methods on a {@code BufferedReader}. Each method will
 542      * simply pick up from where it was left on last read.
 543      *
 544      * <p>If an {@link IOException} is thrown when accessing the underlying
 545      * {@code BufferedReader}, it is wrapped in an {@link
 546      * UncheckedIOException} which will be thrown from the {@code Stream}
 547      * method that caused the read to take place. For example, when trying to
 548      * read from the {@code Stream} after the {@code BufferedReader} is
 549      * closed, will throw an {@code UnchecheckedIOException}.
 550      *
 551      * @return a {@code Stream<String>} providing the lines of text
 552      *         described by this {@code BufferedReader}
 553      *
 554      * @since 1.8
 555      */
 556     public Stream<String> lines() {
 557         Iterator<String> iter = new Iterator<String>() {
 558             String nextLine = null;
 559 
 560             @Override
 561             public boolean hasNext() {
 562                 if (nextLine != null) {
 563                     return true;
 564                 } else {
 565                     try {
 566                         nextLine = readLine();
 567                         return (nextLine != null);
 568                     } catch (IOException e) {
 569                         throw new UncheckedIOException(e);
 570                     }
 571                 }
 572             }
 573 
 574             @Override
 575             public String next() {
 576                 if (nextLine != null || hasNext()) {
 577                     try {
 578                         return nextLine;
 579                     } finally {
 580                         nextLine = null;
 581                     }
 582                 } else {
 583                     throw new NoSuchElementException();
 584                 }
 585             }
 586         };
 587         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iter, Spliterator.ORDERED));
 588     }
 589 }