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:

Một số tính năng mới về xử lý ngoại lệ trong Java 7
Cachable Static Assets with Spring MVC
Java Program to Implement Graph Coloring Algorithm
Spring Cloud AWS – RDS
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Spring WebClient vs. RestTemplate
Java Program to Implement Stack using Linked List
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Using the Not Operator in If Conditions in Java
Hướng dẫn Java Design Pattern – Abstract Factory
Date Time trong Java 8
Tính đa hình (Polymorphism) trong Java
JUnit 5 for Kotlin Developers
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Hướng dẫn Java Design Pattern – Chain of Responsibility
Comparing Two HashMaps in Java
The Thread.join() Method in Java
Java Program for Douglas-Peucker Algorithm Implementation
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Queue và PriorityQueue trong Java
Guide to Apache Commons CircularFifoQueue
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 Hash Tables Chaining with List Heads
Ignore Null Fields with Jackson
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Guide to the Java ArrayList
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Represent Graph Using 2D Arrays
Split a String in Java
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Hướng dẫn Java Design Pattern – State