I am familiar with the concept of InputStream
,buffers and why they are useful (when you need to work with data that might be larger then the machines RAM for example).
I was wondering though, how does the InputStream
actually carry all that data?. Could a OutOfMemoryError
be caused if there is TOO much data being transfered?
If I connect from a client to a server,requesting a 100GB file, the server starts iterating through the bytes of the file with a buffer, and writing the bytes back to the client with outputStream.write(byte[])
. The client is not ready to read the InputStream
right now,for whatever reason. Will the server continue sending the bytes of the file indefinitely? and if so, won't the outputstream/inputstream
be larger than the RAM of one of these machines?
Java InputStream. InputStream is a source for reading data. A stream can represent various kinds of sources, including disk files, devices, other programs, and memory arrays. Streams support many different types of data, including simple bytes, primitive data types, localized characters, and objects.
Here are some of the commonly used methods: markSupported () - checks if the mark () and reset () method is supported in the stream Here is how we can implement InputStream using the FileInputStream class. Suppose we have a file named input.txt with the following content.
read () : java.io.InputStream.read () reads next byte of data from the Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
Applications that are defining subclass of InputStream must provide method, returning the next byte of input. A reset () method is invoked which re-positions the stream to the recently marked position.
InputStream
and OutputStream
implementations do not generally use a lot of memory. In fact, the word "Stream" in these types means that it does not need to hold the data, because it is accessed in a sequential manner -- in the same way that a stream can transfer water between a lake and the ocean without holding a lot of water itself.
But "stream" is not the best word to describe this. It's more like a pipe, because when you transfer data from a server to a client, every stage transfers back-pressure from the client that controls the rate at which data gets sent. This is similar to how your faucet controls the rate of flow through your pipes all the way to the city reservoir:
InputStream
only requests more data from the OS when its internal (small) buffers are empty. Each request allows only a limited amount of data to be transferred;OutputStream
, the OutputStream
will try to write data to the OS. When the OS buffer is full, it will make the server process wait until the server-side buffer has space to accept new data.Notice that a slow client can make the server process take a very long time. If you're writing a server, and you don't control the clients, then it's very important to consider this and to ensure that there are not a lot of server-side resources tied up while a long data transfer takes place.
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