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 Represent Graph Using Adjacency Matrix
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Hướng dẫn Java Design Pattern – State
How to Get the Last Element of a Stream in Java?
Quick Guide to @RestClientTest in Spring Boot
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Implement PriorityBlockingQueue API
Java Program to Implement ConcurrentLinkedQueue API
Java Program to Implement Vector API
Spring REST API with Protocol Buffers
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Create a Random Linear Extension for a DAG
Spring REST API + OAuth2 + Angular
Limiting Query Results with JPA and Spring Data JPA
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
An Example of Load Balancing with Zuul and Eureka
Command-Line Arguments in Java
Registration – Activate a New Account by Email
Java Program to Implement Jarvis Algorithm
Java Program to Implement AA Tree
Spring 5 Functional Bean Registration
Java Program to Implement Pairing Heap
Enum trong java
Jackson Unmarshalling JSON with Unknown Properties
Template Engines for Spring
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
HttpClient Timeout
Hướng dẫn Java Design Pattern – Interpreter
Spring Boot - Google OAuth2 Sign-In
Java Program to Implement a Binary Search Tree using Linked Lists