Colanta
Colanta

Reputation: 87

Stream a batch process into a CSV java 7

Currently I want to steam the result of a series of calls to an external server into a csv download.

List<List<String>> masterList;
for(int i = 0 ; i<numberOfPages ; i++){
    List<List<String>> subList = parseResponse(callServer(int from, int number));
    masterList.addAll(sublist);
}
Filedownload.save(generateCVS(masterList).getBytes());

This is currently working fine, however, when the number of pages is high the file download does not begins until the full object is in memory.

I want to now if its posible and how to generate the byte stream on the fly so the download can start while the masterlist is being generated

thank you

Edit:

callServer does a call to a rest service parseResponse, parses the service responce into a List> generateCSV parses the list into a CSV formated String Filedownload is from Zul Api

Upvotes: 0

Views: 48

Answers (1)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

Your List<List<String>> subList = parseResponse(callServer(int from, int number)); block the subsequent statements so you have to wait.

If you want to stream your response bytes to your local files, you have to access the InputStream from HttpServletResponse. And process your bytes as you read from response

Upvotes: 1

Related Questions