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:
Java Program to Generate Random Numbers Using Probability Distribution Function
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
An Intro to Spring Cloud Vault
A Guide to TreeSet in Java
Java Program to Implement Hash Tables Chaining with List Heads
Lớp lồng nhau trong java (Java inner class)
Java Program to Implement Treap
Introduction to Netflix Archaius with Spring Cloud
Java Program to Implement WeakHashMap API
String Initialization in Java
A Guide to Java HashMap
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Bootstrap a Web Application with Spring 5
Java Program to Check the Connectivity of Graph Using DFS
Giới thiệu Aspect Oriented Programming (AOP)
Spring Cloud Bus
Guide to Java OutputStream
Converting between an Array and a List in Java
How to Get the Last Element of a Stream in Java?
Java – Combine Multiple Collections
Hướng dẫn Java Design Pattern – Decorator
Hướng dẫn Java Design Pattern – State
Guide to the ConcurrentSkipListMap
An Introduction to ThreadLocal in Java
How to Replace Many if Statements in Java
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement Floyd-Warshall Algorithm
Guide to CountDownLatch in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Lớp Collections trong Java (Collections Utility Class)
Filtering and Transforming Collections in Guava
A Guide to ConcurrentMap