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 an Algorithm to Find the Global min Cut in a Graph
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Spring Boot: Customize Whitelabel Error Page
HashSet trong Java hoạt động như thế nào?
DynamoDB in a Spring Boot Application Using Spring Data
Java Program to Implement Nth Root Algorithm
How to Get All Dates Between Two Dates?
Spring Boot - Admin Client
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Java Program to Perform Searching Using Self-Organizing Lists
Multi Dimensional ArrayList in Java
Guide to the Java TransferQueue
Introduction to Spring Data JPA
Guide to java.util.concurrent.Future
Java Program for Topological Sorting in Graphs
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Implement Ternary Heap
Java Program to Perform integer Partition for a Specific Case
Annotation trong Java 8
Java Program to Implement Fenwick Tree
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Check whether Undirected Graph is Connected using BFS
New Stream Collectors in Java 9
So sánh ArrayList và LinkedList trong Java
Control the Session with Spring Security
Custom Error Pages with Spring MVC
An Intro to Spring Cloud Vault
Introduction to Using FreeMarker in Spring MVC
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Implement Selection Sort
Java Program to Implement TreeMap API
Introduction to Spring Method Security