io.javalin.plugin.json.PipedStreamUtil.kt Maven / Gradle / Ivy
The newest version!
package io.javalin.plugin.json
import io.javalin.core.LoomUtil
import java.io.InputStream
import java.io.PipedInputStream
import java.io.PipedOutputStream
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
object PipedStreamUtil {
val executorService = if (LoomUtil.loomAvailable) {
LoomUtil.getExecutorService("PipedStreamThreadPool")
} else {
ThreadPoolExecutor(4, 32, 30L, TimeUnit.SECONDS, LinkedBlockingQueue())
}
fun getInputStream(userCallback: (PipedOutputStream) -> Unit): InputStream {
val pipedOutputStream = PipedOutputStream()
val pipedInputStream = object : PipedInputStream(pipedOutputStream) {
var exception: Exception? = null // possible exception from child thread
override fun close() = exception?.let { throw it } ?: super.close()
}
executorService.execute { // start child thread, necessary to prevent deadlock
try {
userCallback(pipedOutputStream)
} catch (userException: Exception) {
pipedInputStream.exception = userException // pass exception to parent thead
} finally {
pipedOutputStream.close()
}
}
return pipedInputStream
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy