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 Check Cycle in a Graph using Topological Sort
The Spring @Controller and @RestController Annotations
Introduction to Java Serialization
How to Add a Single Element to a Stream
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Comparing Strings in Java
Java Program to Implement Sorted Vector
Java Program to implement Bit Matrix
Java Program to Permute All Letters of an Input String
Spring Data MongoDB – Indexes, Annotations and Converters
Giới thiệu Google Guice – Dependency injection (DI) framework
What is Thread-Safety and How to Achieve it?
Java Program to Solve the Fractional Knapsack Problem
Giới thiệu Java 8
Java Program to Implement Depth-limited Search
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Hướng dẫn Java Design Pattern – Decorator
Uploading MultipartFile with Spring RestTemplate
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Implement Stein GCD Algorithm
Custom Thread Pools In Java 8 Parallel Streams
Java Program to find the peak element of an array using Binary Search approach
ClassNotFoundException vs NoClassDefFoundError
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Implement Flood Fill Algorithm
Java Program to Solve a Matching Problem for a Given Specific Case
An Intro to Spring Cloud Zookeeper
Java Program to Represent Graph Using Incidence Matrix
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists