All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dankito.utils.process.AsyncStreamReader.kt Maven / Gradle / Ivy

There is a newer version: 1.0.20
Show 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)
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy