Skip to content

Instantly share code, notes, and snippets.

@auryn31
Created July 10, 2019 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auryn31/b0d0c13867462c03d43f695dfc6bcbc7 to your computer and use it in GitHub Desktop.
Save auryn31/b0d0c13867462c03d43f695dfc6bcbc7 to your computer and use it in GitHub Desktop.
@Component
class CSVStreamResponseOutput : StreamingResponseBody {
var dataProvider: DataProvider
constructor(dataProvider: DataProvider) {
this.dataProvider = dataProvider
}
/**
* writes every line from the dataprovider to the output
*/
override fun writeTo(os: OutputStream) {
val writer = BufferedWriter(OutputStreamWriter(os))
val countDownLatch = CountDownLatch(1)
dataProvider.getDataStream().subscribe({
writer.write(it)
writer.write("\n")
writer.flush()
}, ::println, {
os.close()
countDownLatch.countDown()
})
countDownLatch.await()
writer.flush()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment