Last active
April 26, 2020 16:29
-
-
Save jagedn/2b53004603922241073f6f8b915f654e to your computer and use it in GitHub Desktop.
how to serve a large file using RxJava2 with Micronaut
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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