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:
Display Auto-Configuration Report in Spring Boot
Iterating over Enum Values in Java
Zipping Collections in Java
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Introduction to PCollections
Lớp Properties trong java
Create a Custom Exception in Java
Java Program to Implement Gaussian Elimination Algorithm
String Operations with Java Streams
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Spring Boot - Enabling HTTPS
Working With Maps Using Streams
Cài đặt và sử dụng Swagger UI
Performance Difference Between save() and saveAll() in Spring Data
Connect through a Proxy
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Guide to Dynamic Tests in Junit 5
The Difference Between map() and flatMap()
Functional Interfaces in Java 8
Java Program to Implement Max Heap
Upload and Display Excel Files with Spring MVC
Guide to the Java Queue Interface
Java Program to Solve TSP Using Minimum Spanning Trees
Call Methods at Runtime Using Java Reflection
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java Program to Implement Cartesian Tree
Java Program to implement Bit Matrix
Java 8 Stream API Analogies in Kotlin
Guide to java.util.Formatter
Java Program to Perform Complex Number Multiplication