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:
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Hướng dẫn Java Design Pattern – Intercepting Filter
Hướng dẫn Java Design Pattern – Visitor
Java Program to Implement the Program Used in grep/egrep/fgrep
Hướng dẫn Java Design Pattern – Iterator
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java InputStream to Byte Array and ByteBuffer
Sending Emails with Java
Java Program to Compute Determinant of a Matrix
Spring Boot Gradle Plugin
DistinctBy in the Java Stream API
Introduction to Spring Data MongoDB
Tính trừu tượng (Abstraction) trong Java
Apache Commons Collections SetUtils
Java Program to Print only Odd Numbered Levels of a Tree
MyBatis with Spring
Java Program to Implement Tarjan Algorithm
Display Auto-Configuration Report in Spring Boot
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Java Program to Evaluate an Expression using Stacks
Java Program to Check whether Undirected Graph is Connected using DFS
Spring Boot Annotations
Java Program to Construct an Expression Tree for an Infix Expression
Hướng dẫn Java Design Pattern – Adapter
Java Program to Implement HashTable API
Use Liquibase to Safely Evolve Your Database Schema
Call Methods at Runtime Using Java Reflection
Java Program to Check Multiplicability of Two Matrices
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Find kth Largest Element in a Sequence