Alright,
I am looking for a way to deliver a continuous stream of bytes to the browser, such as a PDF file, so it can be displayed to the user.
if (bytesToRead == -1) {
bytesToRead = (int)fullPathFile.length();}
byte[] buffer = new byte[bytesToRead];
int bytesRead = -1;
if((inputFileInputStream != null) && ((bytesRead = inputFileInputStream.read(buffer)) != -1)){
if (codec.equals("base64")) {
String streamLength = Base64.encodeBytes(buffer, 0, bytesToRead);
response.setContentLength(streamLength.length());
outputFileOutputStream.write(Base64.encodeBytes(buffer, 0, bytesToRead).getBytes());
} else {
outputFileOutputStream.write(buffer, 0, bytesToRead);
}
}
inputFileInputStream.close();
outputFileOutputStream.flush();
outputFileOutputStream.close();
I have successfully achieved this with the option -1 to get the entire byte stream at once. However, I now want to send smaller chunks of 2kb each. To accomplish this, I understand that I need to send multiple 2kb responses and concatenate them in the JavaScript code. But how exactly do I go about doing this?