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 for Topological Sorting in Graphs
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Java Program to Implement Gauss Seidel Method
Java Program to Perform Sorting Using B-Tree
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to Solve the Fractional Knapsack Problem
Java Program to Implement CopyOnWriteArraySet API
HttpClient with SSL
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Basic Authentication with the RestTemplate
Display Auto-Configuration Report in Spring Boot
Spring Boot - Hystrix
Java Program to do a Depth First Search/Traversal on a graph non-recursively
A Custom Media Type for a Spring REST API
Spring Boot - Database Handling
Java Program to Implement Expression Tree
Spring Data JPA Delete and Relationships
Java Program to Perform Addition Operation Using Bitwise Operators
Spring REST API with Protocol Buffers
Java Program to Implement Sorted Array
Java equals() and hashCode() Contracts
Giới thiệu Google Guice – Injection, Scope
Java Program to Implement AVL Tree
A Guide to JPA with Spring
The DAO with JPA and Spring
Setting a Request Timeout for a Spring REST API
Spring MVC Async vs Spring WebFlux
Giới thiệu Java 8
Java Program to Implement WeakHashMap API
Spring MVC Tutorial
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees