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:
Programmatic Transaction Management in Spring
Java Program to Check Whether Graph is DAG
Java Program to Check whether Directed Graph is Connected using BFS
An Intro to Spring Cloud Vault
Understanding Memory Leaks in Java
Different Ways to Capture Java Heap Dumps
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Apache Commons Collections Bag
Inheritance with Jackson
Spring Boot - Google OAuth2 Sign-In
Spring Cloud AWS – S3
Java Program to Find Basis and Dimension of a Matrix
HttpClient Timeout
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Implement Hash Tables with Quadratic Probing
Most commonly used String methods in Java
Spring 5 Testing with @EnabledIf Annotation
Spring Boot - Internationalization
Java 8 Collectors toMap
Spring Data JPA and Null Parameters
Registration – Activate a New Account by Email
Spring – Injecting Collections
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Guide to the Java TransferQueue
HttpAsyncClient Tutorial
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Spring Security 5 for Reactive Applications
Java Program to Implement Binary Search Tree
Java Program to Show the Duality Transformation of Line and Point
Java Program to Implement Horner Algorithm
Setting the Java Version in Maven
Lớp Collectors trong Java 8