Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use ByteArrayInputStream rather than byte[] in Java

As I understand ByteArrayInputStream is used to read byte[] data. Why should I use it rather than simple byte[] (for example reading it from DB).

What is the different between them?

like image 441
Sahar Menashe Avatar asked Nov 23 '15 06:11

Sahar Menashe


People also ask

What is bytearrayinputstream class in Java?

The ByteArrayInputStream class provides implementations for different methods present in the InputStream class. read () - reads the single byte from the array present in the input stream read (byte [] array) - reads bytes from the input stream and stores in the specified array

How to read byte array as input stream in Java?

The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to read byte array as input stream. Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the data is read from a byte array.

What is the difference between Mark () and read () in bytearrayinputstream?

mark (int arg) : Java.io.ByteArrayInputStream.mark (int arg) marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid. read () : java.io.ByteArrayInputStream.read () reads next byte of data from the Input Stream.

What is the difference between tobytearray () and bytearrayoutputstream ()?

ByteArrayOutputStream has writeTo (OutputStream out) - which writes the complete contents of this byte array output stream to the specified output stream and toByteArray () - creates a newly allocated byte array of same size.


3 Answers

If the input is always a byte[], then you're right, there's often no need for the stream. And if you don't need it, don't use it. One additional advantage of a ByteArrayInputStream is that it serves as a very strong indication that you intend the bytes to be read-only (since the stream doesn't provide an interface for changing them), though it's important to note that a programmer can often still access the bytes directly, so you shouldn't use that in a situation where security is a concern.

But if it's sometimes a byte[], sometimes a file, sometimes a network connection, etc, then you need some sort of abstraction for "a stream of bytes, and I don't care where they come from." That's what an InputStream is. When the source happens to be a byte array, ByteArrayInputStream is a good InputStream to use.

This is helpful in many situations, but to give two concrete examples:

  • You're writing a library that takes bytes and processes them somehow (maybe it's an image processing library, for instance). Users of your library may supply bytes from a file, or from a byte[] in memory, or from some other source. So, you provide an interface that accepts an InputStream — which means that if what they have is a byte[], they need to wrap it in a ByteArrayInputStream.
  • You're writing code that reads a network connection. But to unit test that code, you don't want to have to open up a connection; you want to just supply some bytes in the code. So the code takes an InputStream, and your test provides a ByteArrayInputStream.
like image 113
yshavit Avatar answered Oct 17 '22 08:10

yshavit


  • A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.
  • ByteArrayInputStream is like wrapper which protects underlying array from external modification
  • It has high order read ,mark ,skip functions
  • A stream also has the advantage that you don't have to have all bytes in memory at the same time, which is convenient if the size of the data is large and can easily be handled in small chunks.

    Reference doc

  • Where as if you choose byte[] ,then you have to generate wheels to do reading ,skipping and track current index explicitly

    byte data[] = { 65, 66, 67, 68, 69 }; // data
        for (int index = 0; index < data.length; index++) {
            System.out.print((char) data[index] + "   ");
        }
        int c = 0;
        ByteArrayInputStream bInput = new ByteArrayInputStream(data);
        while ((bInput.read()) != -1) {
            System.out.println(Character.toUpperCase((char) c));
        }
    
like image 6
rupesh_padhye Avatar answered Oct 17 '22 07:10

rupesh_padhye


ByteArrayInputStream is a good wrapper for byte[], the core is understanding stream, a stream is an ordered sequence of bytes of indeterminate length.Input streams move bytes of data into a java program from some generally external source, in java io, you can decorate one stream to another stream to get more function. but the performance maybe bad. the power of the stream metaphor is that difference between these source and destinations are abstracted way,all input and output operations are simply traded as streams using the same class and the same method,you don not learn a new API for every different kind of device, the same API that read file can read network sockets,serial ports, Bluetooth transmissions, and more.

like image 1
LiLi Avatar answered Oct 17 '22 09:10

LiLi