Convert OutputStream to ByteArrayOutputStream

后端 未结 3 1080
青春惊慌失措
青春惊慌失措 2021-02-18 23:59

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 wa

3条回答
  •  醉酒成梦
    2021-02-19 00:34

    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!

提交回复
热议问题