Like a data stream in general, this can be an I/O stream or memory stream or a file stream or what not.
I understand that it’s some sort of stream of bytes, that you can read to parse very large amounts of data if needed, what I don’t understand however, is how it works.
Is it possible to ‘miss’ data from a stream if you start reading it too late? or does it wait for you?
A lot of tutorials I find online just tell how to use a stream, but it doesn’t actually get out of the way to explain how it works, one tutorial explained it like a river, but that just confuses me more; the water running in the river doesn’t wait, which again brings me back to; what if I read it too late?
Can someone explain this concept to me?
The defining trait of a stream is that you can consume it part by part. Typically, a stream API has methods that offer the next chunk, the next byte etc. of content, and you call them in a loop, interleaving processing with the reading of data. The opposite would be a component that only lets you access the entire content at once, e.g. a primitive File API that only has a method that looks like
byte[] getContent()
The disadvantage of non-streamed data access is that you have to store the entire data in memory at one time or other, which may be prohibitively expensive if there is a lot of it.
As ratchet freak explained, streaming access doesn’t mean you won’t miss something if you start reading too late. It only promises that it’s okay in principle to read the data in increments rather than all at once.
whether it is possible to miss some data depends on the source of the data;
- if you start streaming a file then it will always start at the beginning of the file wait for you and you can rewind as needed.
- if you are streaming an internet livestream then you will start getting data from the point you connect. and it won’t wait
you can compare it to listening to audio, you can start/pause/rewind at any point you want on a LP record/cassette/CD but you can’t with radio broadcasts
1