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:
Finding the Differences Between Two Lists in Java
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
How to Define a Spring Boot Filter?
The XOR Operator in Java
Java Program to Solve a Matching Problem for a Given Specific Case
Java String Conversions
Convert String to Byte Array and Reverse in Java
Daemon Threads in Java
Generic Constructors in Java
Split a String in Java
OAuth2 Remember Me with Refresh Token
Spring Boot - Rest Controller Unit Test
Java Program to Perform Stooge Sort
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Spring Boot - Introduction
Java Program to Generate Random Numbers Using Probability Distribution Function
Show Hibernate/JPA SQL Statements from Spring Boot
An Intro to Spring Cloud Task
Java – Random Long, Float, Integer and Double
The Dining Philosophers Problem in Java
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Create the Prufer Code for a Tree
Display Auto-Configuration Report in Spring Boot
Guide to WeakHashMap in Java
Java Program to Compute Cross Product of Two Vectors
Autoboxing và Unboxing trong Java
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Ternary Search Tree
Quick Intro to Spring Cloud Configuration
Java Program to Perform Polygon Containment Test
Creating a Generic Array in Java
HandlerAdapters in Spring MVC