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:
Spring Boot - Servlet Filter
Java Program to Implement Caesar Cypher
A Quick Guide to Spring Cloud Consul
Lớp Arrarys trong Java (Arrays Utility Class)
New Features in Java 10
Spring Boot with Multiple SQL Import Files
Remove All Occurrences of a Specific Value from a List
Tính đóng gói (Encapsulation) trong java
More Jackson Annotations
A Guide to HashSet in Java
Error Handling for REST with Spring
Creating Docker Images with Spring Boot
Java Program to Implement Sieve Of Eratosthenes
Spring Security Remember Me
Service Registration with Eureka
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Compact Strings in Java 9
Java Program to Implement Selection Sort
Spring Boot Configuration with Jasypt
Java Program to Search for an Element in a Binary Search Tree
JUnit 5 for Kotlin Developers
Add Multiple Items to an Java ArrayList
Java Program to Compute the Area of a Triangle Using Determinants
Java Program to Implement Queue using Linked List
Hướng dẫn Java Design Pattern – Iterator
Custom HTTP Header with the HttpClient
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Mệnh đề if-else trong java
Guide to Java Instrumentation