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:
Check if there is mail waiting
Java Program to Implement DelayQueue API
Java Program to Implement Strassen Algorithm
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Find all Forward Edges in a Graph
Spring NoSuchBeanDefinitionException
Hướng dẫn Java Design Pattern – Service Locator
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Jackson vs Gson
Java Program to Implement Binary Search Tree
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Hướng dẫn Java Design Pattern – Strategy
Java Program to Perform integer Partition for a Specific Case
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to Check whether Directed Graph is Connected using BFS
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
What is a POJO Class?
Quick Guide to Spring Bean Scopes
Java Program to Solve Knapsack Problem Using Dynamic Programming
Serverless Functions with Spring Cloud Function
Kết hợp Java Reflection và Java Annotations
Java – Random Long, Float, Integer and Double
Bootstrap a Web Application with Spring 5
The Thread.join() Method in Java
Set Interface trong Java
Java String to InputStream
Spring Security Form Login
Java Program to Implement Flood Fill Algorithm
Java Program to Implement Doubly Linked List
So sánh ArrayList và LinkedList trong Java
Check if a String is a Palindrome in Java
Spring Security 5 for Reactive Applications