Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal block size in java streams

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?

like image 824
Igor Avatar asked Apr 12 '11 03:04

Igor


People also ask

Is Java stream blocking?

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.

What are two types of streams in Java 8?

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.

What is use of stream in Java 8?

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.

What does stream () do in Java?

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.


1 Answers

The answer is: it depends. For a general solution, stop worrying about it and just use a library. Common choices:

  • Apache Commons IO IOUtils#copy() or copyLarge(), or
  • Google Guava's ByteStreams#copy()
like image 167
Matt Ball Avatar answered Sep 19 '22 08:09

Matt Ball