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, "ByteArrayInput
Stream
" 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 byte
s to it post-construction, which might not necessarily do anything for me in this particular case.
So my questions:
- In my use case, are there any benefits to
ByteArrayInputStream
overbyte[]
? Why/why not? What are the factors that drive this decision (in general)? - 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?