Convert InputStream to OutputStream in Java

Learn to convert or pipe an InputStream to OutputStream in Java using various Java APIs, Commons-IO and Guava libraries.

It is recommended to use the try-with-resources statement with Streams. Else use finally statements to close the streams.

1. Using InputStream.transferTo() [Java 9]

The new method transferTo(), in Java 9, reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.

It does not close either stream so it is important to close the streams by other means.

try (InputStream inputStream = new ByteArrayInputStream("howtodoinjava".getBytes());
     OutputStream outputStream = new ByteArrayOutputStream();) {

  inputStream.transferTo(outputStream);
} catch (IOException e) {
  //handle exception
}

2. Writing Directly in Java 8

There is no API similar to transferTo() in Java 8. So we can mimic the logic written in the above API’s source code and write it ourselves.

void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[8192];
    int length;
    while ((length = in.read(buf)) > 0) {
        out.write(buf, 0, length);
    }
}

3. Using Guava ByteStreams.copy()

We can use the ByteStreams.copy() API from transferring the bytes from InputStream to OutputStream.

The ByteStreams class contains many utility methods for working with byte arrays and I/O streams. The copy() method copies all bytes from the input stream to the output stream.

It does not close or flush either stream.

try (InputStream inputStream = new ByteArrayInputStream("howtodoinjava".getBytes());
     OutputStream outputStream = new ByteArrayOutputStream()) {

  ByteStreams.copy(inputStream, outputStream);
} catch (IOException e) {
  //handle exception
}

4. Using Commons IO’s IOUtils.copy()

The IOUtils class provides static utility methods for input/output operations, including convering between streams.

Its copy() method copies bytes from an InputStream to an OutputStream. This method buffers the input internally, so there is no need to use a BufferedInputStream.

try (InputStream inputStream = new ByteArrayInputStream("howtodoinjava".getBytes());
     OutputStream outputStream = new ByteArrayOutputStream()) {

  IOUtils.copy(inputStream, outputStream);
} catch (IOException e) {
  //handle exception
}

For large streams use the copyLarge() method that supports copying large byte array data over 2 GB.

IOUtils.copyLarge(inputStream, outputStream);

4. Conclusion

In this Java IO tutorial, we learned many easy and handy ways to copy the byte array data from InputStream to OutputStream. We learned to convert small as well as large input streams to output streams.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode