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:
Spring Data JPA Delete and Relationships
Format ZonedDateTime to String
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Hamcrest Collections Cookbook
Java Program to Implement Regular Falsi Algorithm
Spring 5 Functional Bean Registration
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Changing Annotation Parameters At Runtime
Encode a String to UTF-8 in Java
Bootstrapping Hibernate 5 with Spring
Login For a Spring Web App – Error Handling and Localization
Intersection of Two Lists in Java
Inject Parameters into JUnit Jupiter Unit Tests
A Guide to LinkedHashMap in Java
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Implement Doubly Linked List
Java Program to Perform Arithmetic Operations on Numbers of Size
Tips for dealing with HTTP-related problems
Guide to the Java ArrayList
Java Program to Implement Trie
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Implement RenderingHints API
The Registration Process With Spring Security
Spring JDBC
Java Program to Find a Good Feedback Edge Set in a Graph
Exploring the Spring 5 WebFlux URL Matching
REST Web service: Basic Authentication trong Jersey 2.x
A Guide to the Java LinkedList
Spring REST API + OAuth2 + Angular
Java – Random Long, Float, Integer and Double
Calling Stored Procedures from Spring Data JPA Repositories