added a little helper method for processing a closeable object which then makes sure the resource is closed (like the try-with-resource extension in Java 7)
This commit is contained in:
@@ -67,6 +67,26 @@ namespace io {
|
|||||||
|
|
||||||
inline fun readLine() : String? = stdin.readLine()
|
inline fun readLine() : String? = stdin.readLine()
|
||||||
|
|
||||||
|
/** Uses the given resource then closes it to ensure its closed again */
|
||||||
|
inline fun <T: Closeable, R> T.use(block: fun(T): R) : R {
|
||||||
|
var closed = false
|
||||||
|
try {
|
||||||
|
return block(this)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
try {
|
||||||
|
this.close()
|
||||||
|
} catch (closeException: Exception) {
|
||||||
|
// eat the closeException as we are already throwing the original cause
|
||||||
|
// and we don't want to mask the real exception
|
||||||
|
}
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
|
if (!closed) {
|
||||||
|
this.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun InputStream.iterator() : ByteIterator =
|
fun InputStream.iterator() : ByteIterator =
|
||||||
object: ByteIterator() {
|
object: ByteIterator() {
|
||||||
override val hasNext : Boolean
|
override val hasNext : Boolean
|
||||||
@@ -95,5 +115,22 @@ namespace io {
|
|||||||
// inline val Reader.buffered : BufferedReader
|
// inline val Reader.buffered : BufferedReader
|
||||||
// get() = if(this is BufferedReader) this else BufferedReader(this)
|
// get() = if(this is BufferedReader) this else BufferedReader(this)
|
||||||
|
|
||||||
|
inline fun Reader.buffered() = BufferedReader(this)
|
||||||
|
|
||||||
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
|
inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize)
|
||||||
|
|
||||||
|
inline fun BufferedReader.lineIterator() : Iterator<String> = LineIterator(this)
|
||||||
|
|
||||||
|
protected class LineIterator(val reader: BufferedReader) : Iterator<String> {
|
||||||
|
var nextLine: String? = null
|
||||||
|
|
||||||
|
override val hasNext: Boolean
|
||||||
|
get() {
|
||||||
|
nextLine = reader.readLine()
|
||||||
|
return nextLine != null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun next(): String = nextLine.sure()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Hello
|
||||||
|
World
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
namespace test.collections
|
||||||
|
|
||||||
|
import std.test.*
|
||||||
|
|
||||||
|
import std.io.*
|
||||||
|
import std.util.*
|
||||||
|
import java.io.*
|
||||||
|
|
||||||
|
class IoTest() : TestSupport() {
|
||||||
|
val file = File("test/HelloWorld.txt")
|
||||||
|
|
||||||
|
fun testUse() {
|
||||||
|
val list = arrayList<String>()
|
||||||
|
|
||||||
|
val reader = FileReader(file).buffered()
|
||||||
|
reader.close()
|
||||||
|
|
||||||
|
/**
|
||||||
|
TODO compiler error?
|
||||||
|
reader.use<BufferedReader,Unit>{
|
||||||
|
val line = it.readLine()
|
||||||
|
if (line != null) {
|
||||||
|
list.add(line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(arrayList("Hello", "World"), list)
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user