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 the Edmond’s Algorithm for Maximum Cardinality Matching
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Spring Boot - Database Handling
Tìm hiểu về Web Service
Java Program to Create a Balanced Binary Tree of the Incoming Data
How to Use if/else Logic in Java 8 Streams
Java Program to Implement TreeMap API
Java Program to Implement Levenshtein Distance Computing Algorithm
MyBatis with Spring
Consumer trong Java 8
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Java Program to Solve Tower of Hanoi Problem using Stacks
Write/Read cookies using HTTP and Read a file from the internet
Derived Query Methods in Spring Data JPA Repositories
Debug a JavaMail Program
Java Program to Implement Range Tree
Java Program to Perform String Matching Using String Library
Spring Boot - Rest Controller Unit Test
Java Program to Perform Left Rotation on a Binary Search Tree
New Features in Java 14
Java – Combine Multiple Collections
Spring JDBC
Period and Duration in Java
Hướng dẫn Java Design Pattern – Adapter
Java Program to Solve any Linear Equations
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Find Nearest Neighbor Using Linear Search
Spring Boot - Unit Test Cases
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Check the Connectivity of Graph Using BFS