Java – InputStream to Reader

In this quick tutorial we’re going to take a look at converting an InputStream to a Reader using Java, then Guava and finally Apache Commons IO.

This article is part of the “Java – Back to Basic” series here on VietMX’s Blog.

1. With Java

First, let’s look at the simple Java solution – using the readily available InputStreamReader:

@Test
public void givenUsingPlainJava_whenConvertingInputStreamIntoReader_thenCorrect() 
  throws IOException {
    InputStream initialStream = new ByteArrayInputStream("With Java".getBytes());
    
    Reader targetReader = new InputStreamReader(initialStream);

    targetReader.close();
}

2. With Guava

Next – let’s take a look at the Guava solution – using an intermediary byte array and String:

@Test
public void givenUsingGuava_whenConvertingInputStreamIntoReader_thenCorrect() 
  throws IOException {
    InputStream initialStream = ByteSource.wrap("With Guava".getBytes()).openStream();
    
    byte[] buffer = ByteStreams.toByteArray(initialStream);
    Reader targetReader = CharSource.wrap(new String(buffer)).openStream();

    targetReader.close();
}

Note that the Java solution is simpler than this approach.

3. With Commons IO

Finally – the solution using Apache Commons IO – also using an intermediary String:

@Test
public void givenUsingCommonsIO_whenConvertingInputStreamIntoReader_thenCorrect() 
  throws IOException {
    InputStream initialStream = IOUtils.toInputStream("With Commons IO");
    
    byte[] buffer = IOUtils.toByteArray(initialStream);
    Reader targetReader = new CharSequenceReader(new String(buffer));

    targetReader.close();
}

And there you have it – 3 quick ways to convert the input stream to a Java Reader. Make sure to check out the sample over on GitHub.

Related posts:

Read an Outlook MSG file
Java Program to Create a Balanced Binary Tree of the Incoming Data
Spring Cloud Bus
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Implement Strassen Algorithm
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Java Timer
Hướng dẫn Java Design Pattern – Strategy
Remove All Occurrences of a Specific Value from a List
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
OAuth2.0 and Dynamic Client Registration
Spring MVC + Thymeleaf 3.0: New Features
A Guide to JPA with Spring
Spring Boot - Admin Client
Simplify the DAO with Spring and Java Generics
So sánh HashMap và Hashtable trong Java
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Implement Skew Heap
Java Program to Print only Odd Numbered Levels of a Tree
Spring REST API + OAuth2 + Angular
The Registration API becomes RESTful
Java Program to Generate Random Numbers Using Multiply with Carry Method
Spring Boot - Exception Handling
So sánh HashMap và HashSet trong Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java – Write to File
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Java Program to Implement Euler Circuit Problem
Java Program to Implement Self Balancing Binary Search Tree