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 CopyOnWriteArrayList
Java – Get Random Item/Element From a List
Converting Java Date to OffsetDateTime
Xử lý ngoại lệ trong Java (Exception Handling)
Java Program to Implement Levenshtein Distance Computing Algorithm
Hướng dẫn Java Design Pattern – Observer
Java Program to Describe the Representation of Graph using Adjacency List
A Guide to Iterator in Java
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Converting String to Stream of chars
Converting Between an Array and a Set in Java
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Implement AVL Tree
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Generate Randomized Sequence of Given Range of Numbers
Intro to Spring Boot Starters
Hướng dẫn Java Design Pattern – Adapter
Java Program to Implement Fermat Primality Test Algorithm
Spring Boot - Actuator
Java Program to Implement Expression Tree
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Affine Cipher
Java Program to Compare Binary and Sequential Search
Spring Boot - Scheduling
Guide to the Java ArrayList
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Find the Connected Components of an UnDirected Graph
Java Program to Check whether Graph is a Bipartite using DFS
Guide to java.util.concurrent.Future
Spring Security Remember Me
Java String Conversions