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:
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Binary Numbers in Java
Java Program to Solve any Linear Equations
Static Content in Spring WebFlux
Toán tử trong java
Java Program to Implement Graph Coloring Algorithm
Java Program to Perform Finite State Automaton based Search
Java Program to Perform the Unique Factorization of a Given Number
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Từ khóa static và final trong java
Java Multi-line String
Migrating from JUnit 4 to JUnit 5
Java Program to Implement Self organizing List
Introduction to Spring Cloud Netflix – Eureka
Java – Generate Random String
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Guava CharMatcher
Java Program to implement Sparse Vector
Testing in Spring Boot
Luồng Daemon (Daemon Thread) trong Java
Hướng dẫn Java Design Pattern – Visitor
Java Program to Find Minimum Element in an Array using Linear Search
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement DelayQueue API
The “final” Keyword in Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
The Guide to RestTemplate
Intersection of Two Lists in Java
Extract network card address
Java Program to Check Cycle in a Graph using Topological Sort
Converting Between a List and a Set in Java