Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Byte array to ByteArrayOutputStream

I need to convert a byte array to ByteArrayOutputStream so that I can display it on screen.

like image 646
Arun Avatar asked Sep 02 '13 14:09

Arun


People also ask

How do you write bytes to ByteArrayOutputStream?

Write Bytes to ByteArrayOutputStream Since the Java ByteArrayOutputStream class is a subclass of the Java OutputStream class, you write bytes to it using the same write methods that OutputStream has: write(int byteToWrite) write(byte[] bytesToWrite, int offset, int length)

How do I create a Bytearray file?

In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file. Example: Java.

What is ByteArrayOutputStream in java?

ByteArrayOutputStream class creates an Output Stream for writing data into byte array. The size of buffer grows automatically as data is written to it. There is no affect of closing the byteArrayOutputStream on the working of it's methods. They can be called even after closing the class.

How do I convert an image to a Bytearray?

Read the image using the read() method of the ImageIO class. Create a ByteArrayOutputStream object. Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class. Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.


1 Answers

byte[] bytes = ....; ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length); baos.write(bytes, 0, bytes.length); 

Method description:

Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

like image 87
Josh M Avatar answered Sep 28 '22 07:09

Josh M