• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

reading a file written to OutputStream

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a method that I need to call that would return a pdf image. But instead of anything being returned from the method, the method writes to a outputstream that is passed as a parameter.

The method signature:
public void getPDFFile(inputParams, OutputStream stream)

My problem is I am again a intermediate application where I need to read the stream and pass it to my calling app as byte array. My calling app cannot pass the outputStream.

How can I achieve this?
Can BufferedOutputStream be useful for this?

Appreciate any help on this..

Thanks.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a small snippet to achieve this.. Can anybody comment if this looks okay..
Or shd I use a different OutputStream..

I am also trying to get info on how my calling app writes the data to the stream.. All I know for now is that I'll get a pdf file thro an OutputStream.

Thanks.
--------------------------

>
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand correctly you are looking to create a PDF file, is there any reason you cannot write directly out to a PDF via FileOutputStream? If you need to read the contents on the PDF and do some sort of manipulation on it you would probably be better served using a PipedOutputStream.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response Don.

The reason I was not using FileOutputStream is because if I understand right, I have to give a file name in that case. Since this is an online application, I dont want to create files in the server. I just want to receive it as bytes and pass it to the calling application.

Please let me know if there is a way I can do this without explicitly using a file name..

thanks.
 
Don Blodgett
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case you will want to use the PipedOutputStream linked to a PipedInputStream. Once you pass the PipedOutputStream instance into the method which takes the OutputStream as an argument, assuming the getPdfFile(String, OutputStream) method is constructed such that it uses a separate Thread to write to the OutputStream, you can then return the PipedInputStream and the user of your method can read from the returned InputStream just as they would any other. If on the other hand the getPdfFile method doesn't write from a separate thread, then you will have to wrap the call to that method in a separate thread created by you, at which point you would have to do a little more exception handling and thread cleanup. Examples of such can be found along with the PipedIn/OutputStream examples through Google.
 
Jay Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again for your response Don.
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Ram wrote:Since this is an online application, I dont want to create files in the server. I just want to receive it as bytes and pass it to the calling application.

Please let me know if there is a way I can do this without explicitly using a file name...



At least two ways. You could write it to a ByteArrayOutputStream, then get the bytes from that. Or quite possibly, if your goal is to send the PDF as the response to an HTTP request, you could just write it directly to the request's output stream. In the latter case you wouldn't have the intermediate step of creating an array of bytes.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic