Convert OutputStream to ByteArrayOutputStream
Asked Answered
T

3

18

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!

Toscanini answered 16/11, 2014 at 19:17 Comment(5)
I didn't think it was necessary to post any code.Toscanini
Unclear answer! Read the whole question please. That example will not compile!Toscanini
The code which writes those 44 bytes must write to your ByteArrayOutputStream in the first place. The code does not be aware that it is writing to a BAOS, it is enough if it relies on the OS class.Arlon
You cannot convert an arbitrary OutputStream into a ByteArrayOutputStream, but you can change the code that gave you the original OutputStream to give you a ByteArrayOutputStream instead.Volscian
That code would be a pain to change.Toscanini
A
27

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.

Arlon answered 16/11, 2014 at 19:25 Comment(6)
Hmmm....there is something about streams in Java that I am just not understanding. Why have an OutputStream (which is the only type that can be written to) and not be able to access the data within? Should they NEVER be used as a variable to hold any kind of data that will be sent to output later on? I am working on an Audio project and most things seem to be using OutputStreams in one way or another. I found some code in the android API that creates a wave file header,just the header, as an OutputStream.Toscanini
After thinking about it, I guess you could append OutputStreams to construct data that is eventually going to an InputStream??? I will read up on Streams and how to use them properly.Toscanini
Yes, you can easily consume the data again once you have it in an array: InputStream is = new ByteArrayInputStream(bos.toByteArray());Arlon
The OutputStream is the type you get presented by "somebody" if you want to write. You use it no matter what is done to the data. If it writes to socket, file, memory, servlet responce, you don't care you always use the write() methods on the OS. The one who calls you is responible to set up the stream in a way that the data goes to somewhere or (in case of BOS) can be retrieved from it. Writing directly ("streaming") is preferred as it does not introduce memeory usage or latency.Arlon
OutputStream$1 cannot be cast to java.io.ByteArrayOutputStreamTinned
@Tinned the a) point starts with a premise, if this does not hold true you cannot use the alternative, that’s why it contains multipleArlon
P
5

You could use the writeTo method of ByteArrayOutputStream.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[8];
bos.write(bytes);
bos.writeTo(oos);

You can create an instance of ByteArrayOutputStream. You then need to write the data to this ByteOutputStream instance and then using the writeTo method, which accepts an OutputStream, you can enable the ByteArrayOutputStream to write the output, to the instance of OutputStream which you passed as the argument.

Hope it works!

Pirouette answered 16/11, 2014 at 19:25 Comment(4)
no, this will write "nothing" (the bos is empty) to oos.Arlon
Do I need to declare the array size just to initialize or will it 'grow' as needed? I assume I should initialize it the length of the outputstream in bytes.Toscanini
You will need to declare the size. Arrays don't grow dynamically.Pirouette
BAOS resizes the backing array automatically (it creates a larger one if needed and copies the bytes over). Btw it might as well manage a list of arrays, like some implementations from Apache do.Arlon
V
-1

You can use toByteArray function on the output stream you have.That's is let say you have outputStream buffer So you can do buffer.toByteArray .

For more you can look at the answer of Convert InputStream to byte array in Java .

Valvulitis answered 16/11, 2014 at 19:28 Comment(4)
The OP wants to make a ByteArrayOutputStream from an OutputStream, you're using an InputStream !Koressa
Regular outputStreams do not have tobytearray method.Toscanini
I had a look on this docs.oracle.com/javase/7/docs/api/java/io/… where it shows toByteArray method.Please go through it once.Valvulitis
@Valvulitis Yes java.io,ByteArrayOutputStream has a toByteArray method, but java.io.OutputStream has not.Arlon

© 2022 - 2024 — McMap. All rights reserved.