1

My current application requires to read data and enable user to download it as a file. Server side code is similar to this : https://stackoverflow.com/a/55788/3192744 I need data inside InputStream, my current method to get InputStream is:

    InputStream getIStream(){
    StringBuilder sb = new StringBuilder();
    String temp = “”;
    while(//there is data to be read from database//){
            temp = //partial data or one entry from database
            //some other modification in temp
            sb.append(temp).append(“\n”);
    }
    exampleString = sb.toString();
    InputStream stream = new ByteArrayInputStream(exampleString.getBytes());
    }

This method works for now since my data is very small, about 1000 String, each of about 100 chars, hence 100000 chars. Still, before I start download, I need to wait for entire data to be written in one string. Also as my data size increases it won't fit in one string. So Is it possible to keep updating InputStream inside the while loop.

And to read this data on server side, I am waiting the method to return InputStream, so is it possible to send InputStream as method argument, and keep reading from it while it is being updated.

Basically I want to implement something like a pipe, where modified strings from database output are being sent, and on the receiving end data is being put in the file for user to download, and download process for user can start while data is being read from database.

Community
  • 1
  • 1
RJN_WLY
  • 335
  • 1
  • 4
  • 18
  • 1
    I don't know if it would solve your problem, but since you seem to be only writing textual data you could use the servlet's `PrinterWriter` instead, calling its `println(String str)` method for each partial data retrieved. In this case you retrieve the `PrinterWriter` through `ServletResponse.getWriter()` instead of using `ServletResponse.getOutputStream()` and don't need to use `StringBuilder` nor `InputStream`. – Aaron Aug 18 '16 at 13:00
  • As @Aaron said, your problem isn't getting an `InputStream`. You should be getting an `OutputStream` (or a `Writer`) where you can output the data as you read it from the database. – Kayaman Aug 18 '16 at 13:07

1 Answers1

1

The problem is that you aggregate all your data into a StringBuilder (and later into the InputStream which you will write to your ServletOutputStream), while you could instead write it along the way.

As mentioned in my comment I suggest using the ServletResponse's PrinterWriter instead of its OutputStream if you don't need to output binary data, but it would work all the same.

With the PrinterWriter:

PrinterWriter writer = ServletResponse.getWriter();
while(/*there is data to be read from database*/){
        temp = //partial data or one entry from database
        //some other modification in temp
        writer.println(temp);
}
writer.close();

With the ServletOutputStream :

OutputStream out = ServletResponse.getOutputStream();
while(/*there is data to be read from database*/){
        temp = //partial data or one entry from database
        //some other modification in temp
        out.write(temp.getBytes());
        out.write('\n');
}
out.close();
Aaron
  • 24,009
  • 2
  • 33
  • 57