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:
Introduction to Thread Pools in Java
The Dining Philosophers Problem in Java
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
How to Store Duplicate Keys in a Map in Java?
Hướng dẫn Java Design Pattern – Adapter
Comparing Two HashMaps in Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Toán tử trong java
Introduction to Using Thymeleaf in Spring
Spring 5 Functional Bean Registration
Generic Constructors in Java
Spring MVC Async vs Spring WebFlux
Check if a String is a Palindrome in Java
The Java 8 Stream API Tutorial
Giới thiệu thư viện Apache Commons Chain
Instance Profile Credentials using Spring Cloud
A Guide to BitSet in Java
Spring Data Reactive Repositories with MongoDB
Java Program to Implement Radix Sort
Spring Web Annotations
Spring Security 5 – OAuth2 Login
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Introduction to Spring Cloud CLI
Using Custom Banners in Spring Boot
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Explain about URL and HTTPS protocol
Java Program to Implement Segment Tree
Biểu thức Lambda trong Java 8 – Lambda Expressions
Java Program to Implement Interval Tree
Java – Generate Random String
Server-Sent Events in Spring