File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Convert OutputStream to byte[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Convert OutputStream to byte[]" Watch "Convert OutputStream to byte[]" New topic
Author

Convert OutputStream to byte[]

Andre Froes
Greenhorn

Joined: Oct 13, 2010
Posts: 18
Hello everyone, how can I convert an OutputStream to byte[]? Im trying to find this information but it is being very hard. I found how to get from others to it, but not from it to others.

Thanks
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
Have you looked at the java.io.ByteArrayOutputStream class
Andre Froes
Greenhorn

Joined: Oct 13, 2010
Posts: 18
One of my attempts is to store an outputstream in it, but i dont know how, I know how to make the inverse of it.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
The ByteArrayOutputStream class is an output stream that outputs to a byte array. Are you saying you have a different type of output stream and you want to send the data written to it to a byte array, if so what type of output stream do you have?

Can you explain exactly what you are trying to achieve.
Andre Froes
Greenhorn

Joined: Oct 13, 2010
Posts: 18
This is the scenario, I am using lowagie librarie to generate pdf, for that, I have to pass an outputstream to it, but I dont want to save the file in the server machine, I want to provide it for instant download, that is why I am trying to convert the outputstream to a byte array. This is the workaround along with my try:



with that kind of file.toString I am receiving the file broken, I want to know if there is a better way.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

As has already been suggested, just replace

with
and replace

with

You can then get the byte array from the ByteArrayOutputStream .
Andre Froes
Greenhorn

Joined: Oct 13, 2010
Posts: 18
Ahh, now I understood what you were saying, indeed, that worked, but the file still comming corrupted, if I set it to a fileoutputstream as I did before specifying a path in local, it work perfectly. Now I have to find what may be breaking it.

btw, thanks a lot for the attention that you are providing me
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Andre Froes wrote:but the file still comming corrupted, D


'still' ? 'corrupted' ? This is the first time you have mentioned these two. Have you closed the ByteArrayOutputStream ? How do you know the output is corrupted ?
 
 
subject: Convert OutputStream to byte[]