|
17 | 17 |
|
18 | 18 | package java.io; |
19 | 19 |
|
| 20 | +import java.util.Iterator; |
| 21 | +import java.util.NoSuchElementException; |
| 22 | +import java.util.Spliterator; |
| 23 | +import java.util.Spliterators; |
| 24 | +import java.util.stream.Stream; |
| 25 | +import java.util.stream.StreamSupport; |
| 26 | + |
20 | 27 | import org.apache.harmony.luni.util.Msg; |
21 | 28 |
|
22 | 29 |
|
@@ -500,4 +507,66 @@ public long skip(long amount) throws IOException { |
500 | 507 | } |
501 | 508 | throw new IllegalArgumentException(); |
502 | 509 | } |
| 510 | + |
| 511 | + /** |
| 512 | + * Returns a {@code Stream}, the elements of which are lines read from |
| 513 | + * this {@code BufferedReader}. The {@link Stream} is lazily populated, |
| 514 | + * i.e., read only occurs during the |
| 515 | + * <a href="../util/stream/package-summary.html#StreamOps">terminal |
| 516 | + * stream operation</a>. |
| 517 | + * |
| 518 | + * <p> The reader must not be operated on during the execution of the |
| 519 | + * terminal stream operation. Otherwise, the result of the terminal stream |
| 520 | + * operation is undefined. |
| 521 | + * |
| 522 | + * <p> After execution of the terminal stream operation there are no |
| 523 | + * guarantees that the reader will be at a specific position from which to |
| 524 | + * read the next character or line. |
| 525 | + * |
| 526 | + * <p> If an {@link IOException} is thrown when accessing the underlying |
| 527 | + * {@code BufferedReader}, it is wrapped in an {@link |
| 528 | + * UncheckedIOException} which will be thrown from the {@code Stream} |
| 529 | + * method that caused the read to take place. This method will return a |
| 530 | + * Stream if invoked on a BufferedReader that is closed. Any operation on |
| 531 | + * that stream that requires reading from the BufferedReader after it is |
| 532 | + * closed, will cause an UncheckedIOException to be thrown. |
| 533 | + * |
| 534 | + * @return a {@code Stream<String>} providing the lines of text |
| 535 | + * described by this {@code BufferedReader} |
| 536 | + * |
| 537 | + * @since 1.8 |
| 538 | + */ |
| 539 | + public Stream<String> lines() { |
| 540 | + Iterator<String> iter = new Iterator<String>() { |
| 541 | + String nextLine = null; |
| 542 | + |
| 543 | + @Override |
| 544 | + public boolean hasNext() { |
| 545 | + if (nextLine != null) { |
| 546 | + return true; |
| 547 | + } else { |
| 548 | + try { |
| 549 | + nextLine = readLine(); |
| 550 | + return (nextLine != null); |
| 551 | + } catch (IOException e) { |
| 552 | + throw new UncheckedIOException(e); |
| 553 | + } |
| 554 | + } |
| 555 | + } |
| 556 | + |
| 557 | + @Override |
| 558 | + public String next() { |
| 559 | + if (nextLine != null || hasNext()) { |
| 560 | + String line = nextLine; |
| 561 | + nextLine = null; |
| 562 | + return line; |
| 563 | + } else { |
| 564 | + throw new NoSuchElementException(); |
| 565 | + } |
| 566 | + } |
| 567 | + }; |
| 568 | + return StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
| 569 | + iter, Spliterator.ORDERED | Spliterator.NONNULL), false); |
| 570 | + } |
| 571 | + |
503 | 572 | } |
0 commit comments