I have the answer. You mustn't rely on InputStream.available() nor should you rely on InputStream.read(byte[] b) getting all the data all the time. It only gets the data that are "available" at that time. More data may become available after the read. It's asynchronous on some level. Better is to read from the InputStream in a loop until you find the end of the file, which is determined by InputStream.read(byte[] b) returning -1. This works for me:
ServletOutputStream oOutput = response.getOutputStream();
InputStream oDocInputStream = oDocument.getInputStream();
byte[] buffer = new byte[(int)oDocument.length()];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = oDocInputStreamread(buffer)) != -1)
baos.write(buffer, 0, bytesRead);
oDocInputStream.close();
oOutput.write(baos.toByteArray());
Hope this helps someone avoid the time loss that I experienced on this one.
Jared Langdon
North America
Europe, Middle East and Africa
Asia Pacific