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,
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.
You can simply iterate the byte array and print the byte using System. out. println() method.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With