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

datamaintain.core.util.ProcessUtil.kt Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package datamaintain.core.util

import datamaintain.core.exception.DatamaintainProcessException
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.util.concurrent.TimeUnit

fun List.runProcess(
        workingDir: File = File("."),
        timeoutValue: Long = 60,
        timeoutUnit: TimeUnit = TimeUnit.MINUTES,
        outputReadFunction: ((InputStream) -> Unit)? = null
): Int {
    try {
        val proc = ProcessBuilder(*this.toTypedArray())
                .directory(workingDir)
                .redirectOutput(ProcessBuilder.Redirect.PIPE)
                .redirectError(ProcessBuilder.Redirect.PIPE)
                .start()
        if (outputReadFunction != null) {
            outputReadFunction(proc.inputStream)
        }
        proc.waitFor(timeoutValue, timeoutUnit)
        return  proc.exitValue()
    } catch (e: IOException) {
        throw DatamaintainProcessException(this, e.message ?: "")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy