Skip to content

Instantly share code, notes, and snippets.

@jagedn
Last active April 26, 2020 16:29
Show Gist options
  • Save jagedn/2b53004603922241073f6f8b915f654e to your computer and use it in GitHub Desktop.
Save jagedn/2b53004603922241073f6f8b915f654e to your computer and use it in GitHub Desktop.
how to serve a large file using RxJava2 with Micronaut
@Controller("/test")
class FileController{
@Get(value = "/{file}", produces = MediaType.IMAGE_GIF)
Flowable<byte[]> image(@PathVariable String file) {
Flowable.create({ emitter ->
new File(file).withInputStream{ inputStream ->
int size=1024
byte[]buff = inputStream.readNBytes(size)
while( buff.length == size){
emitter.onNext(buff)
buff = inputStream.readNBytes(size)
}
emitter.onNext(buff)
emitter.onComplete()
}
}, BackpressureStrategy.BUFFER)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment