Chunked Encoding vs writing on HttpServletResponse outputStream?

804 Views Asked by At

What benefit does chunked encoding provide over directly writing on the HttpServletResponse output stream in small chunks?

So doing something like the code shown below where the full file content is not being buffered in the app-memory vs sending with chunked encoding header. I'm using IoUtils.copy below which will only read 8K bytes from the input stream at a time.

@GetMapping("/content")
public void getContent(final HttpServletRequest request,
                        final HttpServletResponse response) throws IOException {
    try(final InputStream is = FileUtils.openInputStream(new File("src/main/resources/test_file.txt"))) {
        //Do something with is
        response.setHeader("ContentType", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setContentLength(70000);
        IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();
    }
}
0

There are 0 best solutions below