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 Insertion Sort
Java Program to Implement Graph Structured Stack
Command-Line Arguments in Java
Java Program to Implement Bloom Filter
Spring Boot - Bootstrapping
Cơ chế Upcasting và Downcasting trong java
Custom Thread Pools In Java 8 Parallel Streams
REST Pagination in Spring
Spring Boot - OAuth2 with JWT
Java Program to Implement Warshall Algorithm
Running Spring Boot Applications With Minikube
Spring Data JPA and Null Parameters
Java Program to Implement Horner Algorithm
Jackson – Bidirectional Relationships
Jackson – Marshall String to JsonNode
Using Optional with Jackson
Java Program to Implement ConcurrentSkipListMap API
Java Program to Implement the RSA Algorithm
Apache Commons Collections SetUtils
Java Program to Implement Sorted Circularly Singly Linked List
Java IO vs NIO
Hướng dẫn sử dụng Java Reflection
A Guide to Concurrent Queues in Java
Understanding Memory Leaks in Java
Guide to Java 8’s Collectors
Java Program to Implement Repeated Squaring Algorithm
Batch Processing with Spring Cloud Data Flow
Java – Reader to String
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Apache Commons Collections BidiMap
JUnit5 Programmatic Extension Registration with @RegisterExtension
How To Serialize and Deserialize Enums with Jackson