changeset 6385:94d64473e8e6

Add BufferedReader.lines
author briangoetz
date Sun, 11 Nov 2012 17:44:44 -0500
parents b904c63a9e03
children 62f516761797
files src/share/classes/java/io/BufferedReader.java src/share/classes/java/io/Reader.java src/share/classes/java/io/UncheckedIOException.java
diffstat 3 files changed, 109 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/io/BufferedReader.java	Fri Nov 09 13:32:54 2012 -0800
+++ b/src/share/classes/java/io/BufferedReader.java	Sun Nov 11 17:44:44 2012 -0500
@@ -26,6 +26,12 @@
 package java.io;
 
 
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.streams.Stream;
+import java.util.streams.StreamOpFlags;
+import java.util.streams.Streams;
+
 /**
  * Reads text from a character-input stream, buffering characters so as to
  * provide for the efficient reading of characters, arrays, and lines.
@@ -78,6 +84,9 @@
     /** The skipLF flag when the mark was set */
     private boolean markedSkipLF = false;
 
+    /** Cached Stream returned from lines() */
+    private Stream<String> linesStream = null;
+
     private static int defaultCharBufferSize = 8192;
     private static int defaultExpectedLineLength = 80;
 
@@ -522,4 +531,57 @@
             }
         }
     }
+
+    /**
+     * Returns a <code>{@link Stream}</code> describing lines read from this <code>BufferedReader</code>.
+     * The <code>Stream</code> is lazily populated via calls to <code>{@link readLine()}</code>.  Multiple
+     * calls to <code>lines()</code> on a given <code>BufferedReader</code> will result in returning the
+     * same <code>Stream</code> object.
+     *
+     * If an <code>IOException</code> is thrown when accessing the underlying <code>BufferedReader</code>, it
+     * is wrapped in an <code>{@link UncheckedIOException}</code> which will be thrown from the <code>Stream</code>
+     * method that caused the read to take place.
+     *
+     * @return a <code>Stream&lt;String&gt;</code> containing the lines of text described by this <code>BufferedReader</code>
+     */
+    public Stream<String> lines() {
+        synchronized (lock) {
+            if (linesStream == null) {
+                Iterator<String> iter = new Iterator<String>() {
+                    String nextLine = null;
+
+                    @Override
+                    public boolean hasNext() {
+                        if (nextLine != null)
+                            return true;
+                        else {
+                            try {
+                                nextLine = readLine();
+                            }
+                            catch (IOException e) {
+                                throw new UncheckedIOException(e);
+                            }
+                            return (nextLine != null);
+                        }
+                    }
+
+                    @Override
+                    public String next() {
+                        if (nextLine == null || hasNext()) {
+                            try {
+                                return nextLine;
+                            }
+                            finally {
+                                nextLine = null;
+                            }
+                        }
+                        else
+                            throw new NoSuchElementException();
+                    }
+                };
+                linesStream = Streams.stream(iter, StreamOpFlags.IS_ORDERED);
+            }
+            return linesStream;
+        }
+    }
 }
--- a/src/share/classes/java/io/Reader.java	Fri Nov 09 13:32:54 2012 -0800
+++ b/src/share/classes/java/io/Reader.java	Sun Nov 11 17:44:44 2012 -0500
@@ -57,7 +57,7 @@
      * the object in this field rather than <tt>this</tt> or a synchronized
      * method.
      */
-    protected Object lock;
+    protected final Object lock;
 
     /**
      * Creates a new character-stream reader whose critical sections will
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/io/UncheckedIOException.java	Sun Nov 11 17:44:44 2012 -0500
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.io;
+
+/**
+ * Wraps an <code>{@link IOException}</code> with an unchecked exception
+ *
+ * @see     java.io.IOException
+ * @since   JDK1.8
+ */
+public class UncheckedIOException extends RuntimeException {
+    public UncheckedIOException(String message, IOException cause) {
+        super(message, cause);
+    }
+
+    public UncheckedIOException(IOException cause) {
+        super(cause);
+    }
+
+    @Override
+    public IOException getCause() {
+        return (IOException) super.getCause();
+    }
+}