Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get internal byte array from ByteArrayInputStream

Tags:

java

I want to get the internal byte array from ByteArrayInputStream. I don't want to extends that class or write it into another byte array. Is there a utility class that help me do that?

Thanks,

like image 976
Sean Nguyen Avatar asked Nov 30 '10 16:11

Sean Nguyen


People also ask

How can I get data from ByteArrayInputStream?

1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read int the form of an integer and if the input stream is ended this method return -1. This method reads one byte at a time from the stream.

How do I print a byte array?

You can simply iterate the byte array and print the byte using System. out. println() method.

What is ByteArrayInputStream and ByteArrayOutputStream?

A ByteArray is an array of bytes. The ByteArrayInputStream reads this Byte array as an input stream, and ByteArrayOutputStream writes data into this Byte array. Both contain an internal buffer to store data and an internal counter to keep track of the next byte to be read or written.


1 Answers

You can not get access to the same byte array, but you can easily copy the contents of the stream:

public byte[] read(ByteArrayInputStream bais) {
     byte[] array = new byte[bais.available()];
     bais.read(array);

     return array;
}
like image 154
Kurt Kaylor Avatar answered Oct 26 '22 22:10

Kurt Kaylor