Table of Contents
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 PriorityQueue API
Unsatisfied Dependency in Spring
Spring MVC Setup with Kotlin
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Binary Numbers in Java
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Find Minimum Element in an Array using Linear Search
Java 14 Record Keyword
Running Spring Boot Applications With Minikube
Java Program to Perform the Sorting Using Counting Sort
Java Program to Perform Left Rotation on a Binary Search Tree
Java Program to Implement Heap
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Quick Intro to Spring Cloud Configuration
Java Program to Implement Nth Root Algorithm
Supplier trong Java 8
The Spring @Controller and @RestController Annotations
Java Program to Implement Binomial Heap
Spring Boot - Servlet Filter
JUnit5 @RunWith
Spring Boot Change Context Path
Java Program to Implement Circular Singly Linked List
Hướng dẫn Java Design Pattern – Strategy
HttpClient Basic Authentication
Queue và PriorityQueue trong Java
Spring Web Annotations
Convert String to int or Integer in Java
A Guide to EnumMap
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Java Program to Implement CopyOnWriteArraySet API