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:

Java Program to Implement Borwein Algorithm
Compare Two JSON Objects with Jackson
Java Program to Implement Ternary Search Tree
Java Program to Implement Ternary Tree
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Implement Max Heap
HttpAsyncClient Tutorial
Adding a Newline Character to a String in Java
Versioning a REST API
Spring Security Registration – Resend Verification Email
Java Program to Perform Deletion in a BST
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Connect through a Proxy
Java Program to Implement RoleList API
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Describe the Representation of Graph using Incidence List
Lập trình đa luồng với CompletableFuture trong Java 8
Spring 5 Functional Bean Registration
HttpClient 4 – Follow Redirects for POST
Java 8 Predicate Chain
Java Program to Perform Arithmetic Operations on Numbers of Size
Quick Guide to @RestClientTest in Spring Boot
Wrapper Classes in Java
Deploy a Spring Boot App to Azure
Java Program to Perform Searching in a 2-Dimension K-D Tree
Java Program to Implement Range Tree
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Functional Interface trong Java 8
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Converting Between an Array and a Set in Java