From ac276e08ba3c7a481e16626b6ccb23eece8bfdca Mon Sep 17 00:00:00 2001 From: James Strachan Date: Thu, 5 Jan 2012 09:10:11 +0000 Subject: [PATCH] avoid double close and added TODO about supporting http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions --- stdlib/ktSrc/JavaIo.kt | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/stdlib/ktSrc/JavaIo.kt b/stdlib/ktSrc/JavaIo.kt index 13bb82482a2..5897db01e3f 100644 --- a/stdlib/ktSrc/JavaIo.kt +++ b/stdlib/ktSrc/JavaIo.kt @@ -2,6 +2,7 @@ package std.io import java.io.* import java.nio.charset.* +import java.util.NoSuchElementException inline fun print(message : Any?) { System.out?.print(message) } inline fun print(message : Int) { System.out?.print(message) } @@ -146,14 +147,25 @@ inline fun Reader.useLines(block: (Iterator) -> T): T = this.buffere inline fun BufferedReader.lineIterator() : Iterator = LineIterator(this) protected class LineIterator(val reader: BufferedReader) : Iterator { - private var nextLine: String? = null + private var nextValue: String? = null + private var done = false override val hasNext: Boolean get() { - nextLine = reader.readLine() - return nextLine != null + if (nextValue == null && !done) { + nextValue = reader.readLine() + if (nextValue == null) done = true + } + return nextValue != null } - override fun next(): String = nextLine.sure() + override fun next(): String { + if (!hasNext) { + throw NoSuchElementException() + } + val answer = nextValue + nextValue = null + return answer.sure() + } }