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:
The “final” Keyword in Java
Java Program to Implement Horner Algorithm
Introduction to Spring Data MongoDB
Using Spring ResponseEntity to Manipulate the HTTP Response
Guide to Guava Multimap
Tổng quan về ngôn ngữ lập trình java
Spring WebFlux Filters
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
A Guide to WatchService in Java NIO2
Java Program to Implement Meldable Heap
Using the Map.Entry Java Class
Send an email using the SMTP protocol
Java Program to Implement String Matching Using Vectors
Java – Rename or Move a File
Java Program to Perform Arithmetic Operations on Numbers of Size
Spring MVC Setup with Kotlin
Java Program to Find the Minimum value of Binary Search Tree
Handling Errors in Spring WebFlux
Changing Annotation Parameters At Runtime
Spring Security 5 – OAuth2 Login
Guide to Mustache with Spring Boot
Using a Spring Cloud App Starter
Java Program to Implement Sorted Doubly Linked List
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Implement Hash Tables Chaining with Binary Trees
The DAO with JPA and Spring
Java String to InputStream
Java Program to Check whether Graph is a Bipartite using BFS
Configure a Spring Boot Web Application
How to Set TLS Version in Apache HttpClient
Guide to DelayQueue
Generate Spring Boot REST Client with Swagger