Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert OutputStream to ByteArrayOutputStream

I am trying to convert an OutputStream to a ByteArrayOutput Stream. I was unable to find any clear simple answers on how to do this. This question was asked in the title of the question on StackOverflow, but the body of the question aske how to change a ByteArrayStream to OuputStream. I have an OutputStream that is already created and this example given in the answer will not compile!

That Question is Here

I have an OutputStream that is already constructed and has a length of 44 bytes called waveHeader. I want to convert that to a ByteArrayOutputStream because I want to be able to change that into a byte[] with waveHeader.ToByteArray() for simplicity in later processes;

Is there a simple type of casting or something that will allow this?

If not then:

  • Is there a way to construct a pointer to the data in the original OutputStream if it is not possible to convert it?

  • How would someone go about accessing the data that is contained in the OutputStream?

I am new to JAVA. This is just a hobby for me. Streams In VisualBasic .net where much easier!

like image 837
Bill Smith Avatar asked Nov 16 '14 19:11

Bill Smith


People also ask

How do I create a ByteArrayOutputStream file?

Once we import the package, here is how we can create an output stream. // Creates a ByteArrayOutputStream with default size ByteArrayOutputStream out = new ByteArrayOutputStream(); Here, we have created an output stream that will write data to an array of bytes with default size 32 bytes.

How do you get ByteArrayOutputStream bytes?

The Java ByteArrayOutputStream class, java. io. ByteArrayOutputStream of the Java IO API enables you to capture data written to a stream in a byte array. You write your data to the ByteArrayOutputStream and when you are done you call the its toByteArray() method to obtain all the written data in a byte array.

What is a ByteArrayOutputStream?

Java ByteArrayOutputStream class is used to write common data into multiple files. In this stream, the data is written into a byte array which can be written to multiple streams later. The ByteArrayOutputStream holds a copy of data and forwards it to multiple streams.


1 Answers

There are multiple possible scenarios:

a) You have a ByteArrayOutputStream, but it was declared as OutputStream. Then you can do a cast like this:

void doSomething(OutputStream os)
{
    // fails with ClassCastException if it is not a BOS
    ByteArrayOutputStream bos = (ByteArrayOutputStream)os;
...

b) if you have any other type of output stream, it does not really make sense to convert it to a BOS. (You typically want to cast it, because you want to access the result array). So in this case you simple set up a new stream and use it.

void doSomething(OutputStream os)
{
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(something);
    bos.close();
    byte[] arr = bos.toByteArray();
    // what do you want to do?
    os.write(arr); // or: bos.writeTo(os);
...

c) If you have written something to any kind of OutputStream (which you do not know what it is, for example because you get it from a servlet), there is no way to get that information back. You must not write something you need later. A solution is the answer b) where you write it in your own stream, and then you can use the array for your own purpose as well as writing it to the actual output stream.

Keep in mind ByteArrayOutputStreams keep all Data in Memory.

like image 161
eckes Avatar answered Oct 07 '22 23:10

eckes