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:

1
2
3
4
5
6
7
8
9
@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:

1
2
3
4
5
6
7
8
9
10
@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:

1
2
3
4
5
6
7
8
9
10
@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 Sieve Of Eratosthenes
A Guide to the Java LinkedList
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to find the peak element of an array using Binary Search approach
Receive email by java client
XML Serialization and Deserialization with Jackson
Extra Login Fields with Spring Security
Java Program to Show the Duality Transformation of Line and Point
How to Read HTTP Headers in Spring REST Controllers
Spring WebClient and OAuth2 Support
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Implement Gale Shapley Algorithm
Java – Get Random Item/Element From a List
Java Program to Implement Fermat Factorization Algorithm
Properties with Spring and Spring Boot
Java Program to Implement Sorted List
Request Method Not Supported (405) in Spring
Java Program for Topological Sorting in Graphs
Java Program to Implement Affine Cipher
Sending Emails with Java
Hướng dẫn Java Design Pattern – Template Method
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement LinkedBlockingQueue API
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Transaction Propagation and Isolation in Spring @Transactional
Hướng dẫn Java Design Pattern – Intercepting Filter
Mệnh đề Switch-case trong java
Quick Guide to @RestClientTest in Spring Boot
Java Program to Implement Strassen Algorithm
Java Program to Implement Jarvis Algorithm
An Intro to Spring Cloud Security