Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

how to save a Document to a byte array using InputStream?


Former Community Member
Dear All,

I have an instance of the com.adobe.idp.Document class.

When I try to save its content to a file using the copyToFile method,

everything works fine. However when I try to save the content of the

document in an array of bytes (using getInputStream method), only part

of the document is stored. In the middle of the document content I

reach end of the stream. Here is a fragment of the source code:


/* rightsEnabledDoc is an instance of com.adobe.idp.Document */

InputStream inStream = rightsEnabledDoc.getInputStream();

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

int readByte;

while((readByte = inStream.read()) != -1) {



return outStream.toByteArray();


The byte array returned by the above code fragment contains only

part of the pdf document content.

What am I doing wrong? Is it possible to read all of the document

content using InputStream?

Any help highly appreciated.


3 Replies


Level 6

I am having this exact problem, but only when I call my orchestration remotely by SOAP.  When I make the call on the same server where LiveCycle resides it's fine.  Anyone??  I'm using LC 8.2.


Level 6

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);



Hope this helps someone avoid the time loss that I experienced on this one.

Jared Langdon


Level 2

Thank you for that solution, it saved me the time to figure it out by myself.