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:
Mapping a Dynamic JSON Object with Jackson
Java Program to Implement Adjacency Matrix
LinkedHashSet trong java
Spring Security OAuth2 – Simple Token Revocation
Java Program to Implement Nth Root Algorithm
Adding Shutdown Hooks for JVM Applications
Introduction to Java 8 Streams
Java Program to Implement HashTable API
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Java Program to implement Circular Buffer
Using a List of Values in a JdbcTemplate IN Clause
Using a Mutex Object in Java
Java – Reader to InputStream
Mockito and JUnit 5 – Using ExtendWith
Testing in Spring Boot
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Implement Hopcroft Algorithm
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Guide to Selenium with JUnit / TestNG
Set Interface trong Java
Tránh lỗi NullPointerException trong Java như thế nào?
Kết hợp Java Reflection và Java Annotations
Spring Boot - Tomcat Port Number
Format ZonedDateTime to String
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Check whether Directed Graph is Connected using BFS
Guide to the Java TransferQueue
Java Program to Check Whether Graph is DAG
Guide to Mustache with Spring Boot
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
HttpClient Timeout