Best way to Pipe InputStream to OutputStream [duplicate]

Java 9

Since Java 9 one can use this method from InputStream:

public long transferTo(OutputStream out) throws IOException

Pre Java 9

A one-liner from apache commons:

IOUtils.copy(inputStream, outputStream);

Documentation here. There are multiple copy methods with different parameters. It is also possible to specify the buffer size.

Leave a Comment