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:
Deploy a Spring Boot App to Azure
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Get the workstation name or IP
Connect through a Proxy
HttpClient 4 – Send Custom Cookie
Immutable ArrayList in Java
Hướng dẫn Java Design Pattern – Singleton
Java Program to Find the Vertex Connectivity of a Graph
Java Program to Solve the Fractional Knapsack Problem
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Scanner hasNext() vs. hasNextLine()
Spring MVC Tutorial
Explain about URL and HTTPS protocol
Java Program to Implement CountMinSketch
Java Program to Implement Queue using Two Stacks
Removing Elements from Java Collections
Spring Security 5 – OAuth2 Login
Lập trình đa luồng với Callable và Future trong Java
Comparing Strings in Java
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement Floyd Cycle Algorithm
Validate email address exists or not by Java Code
Java – Combine Multiple Collections
Java Program to Implement Red Black Tree
Returning Custom Status Codes from Spring Controllers
Spring Boot - Eureka Server
Marker Interface trong Java
Overview of the java.util.concurrent
Java Program to Implement LinkedBlockingDeque API
Jackson – Change Name of Field
Hướng dẫn sử dụng Printing Service trong Java
Remove All Occurrences of a Specific Value from a List