avoid double close and added TODO about supporting http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions
This commit is contained in:
+16
-4
@@ -2,6 +2,7 @@ package std.io
|
|||||||
|
|
||||||
import java.io.*
|
import java.io.*
|
||||||
import java.nio.charset.*
|
import java.nio.charset.*
|
||||||
|
import java.util.NoSuchElementException
|
||||||
|
|
||||||
inline fun print(message : Any?) { System.out?.print(message) }
|
inline fun print(message : Any?) { System.out?.print(message) }
|
||||||
inline fun print(message : Int) { System.out?.print(message) }
|
inline fun print(message : Int) { System.out?.print(message) }
|
||||||
@@ -146,14 +147,25 @@ inline fun <T> Reader.useLines(block: (Iterator<String>) -> T): T = this.buffere
|
|||||||
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
||||||
|
|
||||||
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
|
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
|
||||||
private var nextLine: String? = null
|
private var nextValue: String? = null
|
||||||
|
private var done = false
|
||||||
|
|
||||||
override val hasNext: Boolean
|
override val hasNext: Boolean
|
||||||
get() {
|
get() {
|
||||||
nextLine = reader.readLine()
|
if (nextValue == null && !done) {
|
||||||
return nextLine != null
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user