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:
Count Occurrences of a Char in a String
Deploy a Spring Boot App to Azure
New in Spring Security OAuth2 – Verify Claims
Spring RestTemplate Error Handling
Different Ways to Capture Java Heap Dumps
HttpAsyncClient Tutorial
Guide to Dynamic Tests in Junit 5
Java Program to Find Minimum Element in an Array using Linear Search
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Tránh lỗi NullPointerException trong Java như thế nào?
Spring Boot - Flyway Database
Java String to InputStream
Introduction to the Java NIO2 File API
Java Program to Implement Repeated Squaring Algorithm
Convert char to String in Java
Spring Boot - Sending Email
Spring Boot - Eureka Server
Java Program to Solve any Linear Equation in One Variable
Java Program to Implement Sorted List
New Features in Java 15
wait() and notify() Methods in Java
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Spring Data JPA and Null Parameters
Spring Boot Security Auto-Configuration
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
How to Read a File in Java
Spring Security Custom AuthenticationFailureHandler
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Shuffling Collections In Java
Composition, Aggregation, and Association in Java
Java InputStream to Byte Array and ByteBuffer