Java – Benefits of ByteArrayInputStream vs Byte Array

binarybytedataiojava

I have a scenario where my Java process will be receiving a stream of binary data:

public class DataHandler {
    public void handleData(DATA data) {
        // TODO: Do something with data
    }
}

I am trying to figure out the proper typing for DATA. I could use a byte[]:

public class DataHandler {
    public void handleData(byte[] data) {
        // TODO: Do something with data
    }
}

Or a ByteArrayInputStream:

public class DataHandler {
    public void handleData(ByteArrayInputStream dataStream) {
        // TODO: Do something with dataStream
    }
}

To me, "ByteArrayInputStream" implies a continuous stream of data that is constantly being modified. Based on that, it feels like ByteArrayInputStream is better suited for my use case as it probably adds a bunch of bells and whistles on top of a plain 'ole byte[]. But then I see that its constructor takes byte[] and doesn't allow you to add more bytes to it post-construction, which might not necessarily do anything for me in this particular case.

So my questions:

  1. In my use case, are there any benefits to ByteArrayInputStream over byte[]? Why/why not? What are the factors that drive this decision (in general)?
  2. Is there a better way to represent a true stream of flowing data (which ultimately comes in as bytes) besides the methods I've recommended here?

Best Answer

InputStream itself is already an abstraction of a stream of bytes that can be read. A ByteArrayInputStream is a specific implementation of InputStream that gets the bytes in the stream from an underlying byte array (that's the one the constructor wants you to supply).

For your method here, there is probably not much benefit in having a ByteArrayInputStream, an InputStream definitely would, as clients could then pass all kinds of byte sources to your method, whereas otherwise they'd have to read them in an array themselves. 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.