Skip navigation
Currently Being Moderated

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

Aug 21, 2007 8:48 AM

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) {
outStream.write(readByte);
}

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.

Greetings,
Tomek.
 
Replies
  • Currently Being Moderated
    Dec 18, 2009 12:19 PM   in reply to (habyor)

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Dec 19, 2009 5:15 PM   in reply to Jared Langdon

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 8, 2011 7:18 AM   in reply to Jared Langdon

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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)