I have a theoretical question. Let's imagine you have an InputStream
and an OutputStream
. You nead to copy content from one to the other and you don't know exactly the size of the content you need to transfer. What is the best choice in general of the block size in the write
method?
Java IO's various streams are blocking. That means, that when a thread invokes a read() or write() , that thread is blocked until there is some data to read, or the data is fully written.
With Java 8, Collection interface has two methods to generate a Stream. stream() − Returns a sequential stream considering collection as its source. parallelStream() − Returns a parallel Stream considering collection as its source.
Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
It simply conveys elements from a source such as a data structure, an array, or an I/O channel, through a pipeline of computational operations. Stream is functional in nature. Operations performed on a stream does not modify it's source.
The answer is: it depends. For a general solution, stop worrying about it and just use a library. Common choices:
IOUtils#copy()
or copyLarge()
, orByteStreams#copy()
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