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 Implement Miller Rabin Primality Test Algorithm
Spring REST API with Protocol Buffers
Java – File to Reader
Java Program to Implement Find all Forward Edges in a Graph
Convert a Map to an Array, List or Set in Java
Java Program to Generate N Number of Passwords of Length M Each
Introduction to Spring Cloud Stream
Java Program to Implement Booth Algorithm
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Reversing a Linked List in Java
How to Replace Many if Statements in Java
Spring Security with Maven
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Generate Date Between Given Range
Guide to Selenium with JUnit / TestNG
Java Program to Implement Sieve Of Atkin
Guide to @JsonFormat in Jackson
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Marker Interface trong Java
Java Program to Implement Adjacency List
Java Program to Perform Search in a BST
Java Program to Implement Insertion Sort
Jackson – Bidirectional Relationships
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to Implement Heap
Spring Data Java 8 Support
Java Program to Implement PrinterStateReasons API
The StackOverflowError in Java
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
HttpAsyncClient Tutorial