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:
Multipart Upload with HttpClient 4
Java Program to Implement ScapeGoat Tree
Quick Intro to Spring Cloud Configuration
Getting the Size of an Iterable in Java
Hướng dẫn Java Design Pattern – MVC
Java Program to Implement Hash Tables Chaining with List Heads
Spring Boot - Unit Test Cases
Configure a Spring Boot Web Application
Removing all Nulls from a List in Java
Java program to Implement Tree Set
Get the workstation name or IP
Practical Java Examples of the Big O Notation
Java Program to Perform Searching in a 2-Dimension K-D Tree
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Exploring the Spring 5 WebFlux URL Matching
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Implement Sieve Of Eratosthenes
Java Program to Implement Ternary Search Algorithm
Thao tác với tập tin và thư mục trong Java
Java Program to Implement Max-Flow Min-Cut Theorem
Immutable Map Implementations in Java
Dynamic Proxies in Java
Java Program to Find Transitive Closure of a Graph
So sánh HashMap và Hashtable trong Java
RegEx for matching Date Pattern in Java
Map to String Conversion in Java
Checking for Empty or Blank Strings in Java
Java Program to Implement Merge Sort Algorithm on Linked List
Collection trong java
Java Program to Implement Direct Addressing Tables
Java Program to Implement Self Balancing Binary Search Tree