File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert OutputStream to byte[]

 
Andre Froes
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the java.io.ByteArrayOutputStream class
 
Andre Froes
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As has already been suggested, just replace

with
and replace

with

You can then get the byte array from the ByteArrayOutputStream .
 
Andre Froes
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic