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:
So sánh ArrayList và Vector trong Java
New in Spring Security OAuth2 – Verify Claims
Spring Web Annotations
Cơ chế Upcasting và Downcasting trong java
Java Program to Implement Iterative Deepening
Java Program to Generate Randomized Sequence of Given Range of Numbers
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Inject Parameters into JUnit Jupiter Unit Tests
Sorting Query Results with Spring Data
Jackson JSON Views
Refactoring Design Pattern với tính năng mới trong Java 8
Exploring the New Spring Cloud Gateway
A Guide to EnumMap
Java Program to Implement Direct Addressing Tables
The Registration API becomes RESTful
Java Program to Find Strongly Connected Components in Graphs
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Guide to BufferedReader
Using the Map.Entry Java Class
More Jackson Annotations
Spring Boot - Code Structure
Java Collections Interview Questions
How to Get a Name of a Method Being Executed?
Convert Hex to ASCII in Java
Sắp xếp trong Java 8
A Quick JUnit vs TestNG Comparison
Java Program to Implement LinkedBlockingQueue API
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Stack Memory and Heap Space in Java
Java Program to Generate Random Hexadecimal Byte
ClassNotFoundException vs NoClassDefFoundError