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 Bi Directional Map
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Spring Boot - Runners
Java – Write to File
Call Methods at Runtime Using Java Reflection
Sử dụng CyclicBarrier trong Java
Java Program to Implement Queue using Linked List
Write/Read cookies using HTTP and Read a file from the internet
Assertions in JUnit 4 and JUnit 5
Stack Memory and Heap Space in Java
Logout in an OAuth Secured Application
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Functional Interface trong Java 8
Introduction to Using Thymeleaf in Spring
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Arrays.asList vs new ArrayList(Arrays.asList())
Implementing a Runnable vs Extending a Thread
Hướng dẫn Java Design Pattern – Decorator
Java Program to Compute DFT Coefficients Directly
Java Program to Implement Warshall Algorithm
Java Program to Check whether Graph is a Bipartite using BFS
Collection trong java
Spring’s RequestBody and ResponseBody Annotations
Simple Single Sign-On with Spring Security OAuth2
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Implement Regular Falsi Algorithm
Semaphore trong Java
List Interface trong Java
Working With Maps Using Streams
Guide to the Volatile Keyword in Java
Explain about URL and HTTPS protocol