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 Find Whether a Path Exists Between 2 Given Nodes
Introduction to Spring Cloud OpenFeign
How to Count Duplicate Elements in Arraylist
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Find the Connected Components of an UnDirected Graph
TreeSet và sử dụng Comparable, Comparator trong java
Receive email using IMAP
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Encode a Message Using Playfair Cipher
Java Program to Implement ScapeGoat Tree
Reactive WebSockets with Spring 5
Spring Webflux and CORS
Giới thiệu về Stream API trong Java 8
Java Program to Implement Aho-Corasick Algorithm for String Matching
Spring Boot - Actuator
Java Program to Implement Segment Tree
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Implement Max Heap
Hướng dẫn Java Design Pattern – Object Pool
Java Program to Implement Direct Addressing Tables
Chuyển đổi từ HashMap sang ArrayList
Java Program to Implement Red Black Tree
JWT – Token-based Authentication trong Jersey 2.x
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Construct K-D Tree for 2 Dimensional Data
Redirect to Different Pages after Login with Spring Security
A Guide to Java HashMap
Service Registration with Eureka
Spring Security Remember Me
Java Program to Implement LinkedBlockingDeque API
Hướng dẫn sử dụng Lớp FilePermission trong java
Practical Java Examples of the Big O Notation