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!

6
  • 1
    I didn't think it was necessary to post any code.
    – Bill Smith
    Nov 16, 2014 at 19:19
  • Unclear answer! Read the whole question please. That example will not compile!
    – Bill Smith
    Nov 16, 2014 at 19:23
  • 1
    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.
    – eckes
    Nov 16, 2014 at 19:38
  • 1
    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. Nov 16, 2014 at 20:25
  • That code would be a pain to change.
    – Bill Smith
    Nov 16, 2014 at 20:26

3 Answers 3

28

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.

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.
    – Bill Smith
    Nov 17, 2014 at 4:52
  • 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.
    – Bill Smith
    Nov 17, 2014 at 4:54
  • Yes, you can easily consume the data again once you have it in an array: InputStream is = new ByteArrayInputStream(bos.toByteArray());
    – eckes
    Nov 17, 2014 at 17:57
  • 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.
    – eckes
    Nov 17, 2014 at 17:59
  • OutputStream$1 cannot be cast to java.io.ByteArrayOutputStream
    – aswzen
    Sep 17, 2019 at 8:15
6

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!

5
  • I will give this a try.
    – Bill Smith
    Nov 16, 2014 at 19:27
  • 3
    no, this will write "nothing" (the bos is empty) to oos.
    – eckes
    Nov 16, 2014 at 19:33
  • 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.
    – Bill Smith
    Nov 16, 2014 at 20:53
  • You will need to declare the size. Arrays don't grow dynamically. Nov 17, 2014 at 3:32
  • 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.
    – eckes
    Sep 17, 2019 at 8:40
-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 .

4
  • The OP wants to make a ByteArrayOutputStream from an OutputStream, you're using an InputStream !
    – Charlie
    Nov 16, 2014 at 19:30
  • 3
    Regular outputStreams do not have tobytearray method.
    – Bill Smith
    Nov 16, 2014 at 19:31
  • 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.
    – cauchy
    Nov 16, 2014 at 19:51
  • 2
    @cauchy Yes java.io,ByteArrayOutputStream has a toByteArray method, but java.io.OutputStream has not.
    – eckes
    Feb 9, 2016 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.