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:
Giới thiệu Google Guice – Dependency injection (DI) framework
Error Handling for REST with Spring
Spring REST API with Protocol Buffers
Batch Processing with Spring Cloud Data Flow
Spring Cloud – Securing Services
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Quick Guide to the Java StringTokenizer
Java Program to Implement Levenshtein Distance Computing Algorithm
Java – Reader to InputStream
Transaction Propagation and Isolation in Spring @Transactional
How to Read a File in Java
Java Program to Implement Hamiltonian Cycle Algorithm
Spring 5 WebClient
Lớp lồng nhau trong java (Java inner class)
Guide to System.gc()
Java Program to Implement Doubly Linked List
Java Program to Implement D-ary-Heap
Spring Boot - Logging
Debug a JavaMail Program
Using the Map.Entry Java Class
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Lập trình mạng với java
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to implement Sparse Vector
Notify User of Login From New Device or Location
Java Program to Implement Treap
Static Content in Spring WebFlux
Java Program to Find All Pairs Shortest Path
Java 8 Collectors toMap
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree