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:
Guide to UUID in Java
Spring Boot - Enabling Swagger2
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Spring Boot Tutorial – Bootstrap a Simple Application
Java Program to Find Maximum Element in an Array using Binary Search
Unsatisfied Dependency in Spring
Spring Boot - Zuul Proxy Server and Routing
Exception Handling in Java
Debugging Reactive Streams in Java
Hướng dẫn sử dụng Printing Service trong Java
Checking for Empty or Blank Strings in Java
A Quick Guide to Spring MVC Matrix Variables
Guide to the Volatile Keyword in Java
Java Program to Implement Sieve Of Atkin
Guava – Join and Split Collections
Assert an Exception is Thrown in JUnit 4 and 5
Java Program to Implement HashMap API
Java Program for Douglas-Peucker Algorithm Implementation
Quick Intro to Spring Cloud Configuration
New Features in Java 12
CyclicBarrier in Java
Java Program to Implement Threaded Binary Tree
Java List UnsupportedOperationException
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Giới thiệu Java 8
@DynamicUpdate with Spring Data JPA
Java Program to Implement Quick sort
Java Program to Implement Regular Falsi Algorithm
Java Program to Check whether Undirected Graph is Connected using BFS
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement the One Time Pad Algorithm