net.dankito.utils.process.AsyncStreamReader.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-utils Show documentation
Show all versions of java-utils Show documentation
Some basic utils needed in many projects
The newest version!
package net.dankito.utils.process
import org.slf4j.LoggerFactory
import java.io.InputStream
open class AsyncStreamReader(protected val inputStream: InputStream) : Thread() {
companion object {
private val log = LoggerFactory.getLogger(AsyncStreamReader::class.java)
}
protected val linesField = mutableListOf()
val lines: List
get() {
this.join() // wait till reading from stream is done
return linesField
}
override fun run() {
try {
inputStream.bufferedReader().use { reader ->
reader.forEachLine {
linesField.add(it)
}
}
} catch (e: Exception) {
log.error("Error occurred while reading stream", e)
}
}
}