Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor implementation of StreamHelp.lines method

introduce FetchIterator
  • Loading branch information...
commit 6cf13dc496a773d76e798c040df94ef44096a8b0 1 parent 81c35e7
Stepan Koltsov authored
Showing with 28 additions and 17 deletions.
  1. +28 −17 src/scalax/io/streams.scala
View
45 src/scalax/io/streams.scala
@@ -15,6 +15,29 @@ import java.io._
import java.util.zip._
import scalax.data._
+private[io] abstract class FetchIterator[T] extends Iterator[T] {
+
+ var fetched = false
+ var nextItem : Option[T] = None
+
+ protected def fetchNext(): Option[T]
+
+ override def hasNext = {
+ if (!fetched) {
+ nextItem = fetchNext()
+ fetched = true
+ }
+ nextItem.isDefined
+ }
+
+ override def next() = {
+ if (!hasNext) throw new NoSuchElementException("EOF")
+ fetched = false
+ nextItem.get
+ }
+
+}
+
class InputStreamExtras(s : InputStream) {
def slurp() = StreamHelp.slurp(s)
def pumpTo(d : OutputStream) = StreamHelp.pump(s, d)
@@ -76,24 +99,12 @@ object StreamHelp
* Iterates over the lines of the reader.
* Keeps reader open even after reaching EOF, reader must be closed explicitly.
*/
- def lines(br : BufferedReader) = new Iterator[String] {
- var fetched = false
- var nextLine : String = _
-
- def hasNext = {
- if (!fetched) {
- nextLine = br.readLine()
- fetched = true
+ def lines(br : BufferedReader): Iterator[String] = new FetchIterator[String] {
+ override def fetchNext() =
+ br.readLine() match {
+ case null => None
+ case s => Some(s)
}
- nextLine ne null
- }
-
- def next() = {
- if (!hasNext) throw new NoSuchElementException("EOF")
- fetched = false
- nextLine
- }
-
}
/** Iterates over the lines of the reader. */
Please sign in to comment.
Something went wrong with that request. Please try again.