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 Perform the Shaker Sort
JPA/Hibernate Persistence Context
Java Program to Implement Graph Coloring Algorithm
Java Program to Implement Nth Root Algorithm
Spring 5 WebClient
Send email with SMTPS (eg. Google GMail)
Java Program to Implement Pollard Rho Algorithm
Guide to Guava Table
Java Program to Perform Searching Based on Locality of Reference
Read an Outlook MSG file
Java toString() Method
Guide to Guava Multimap
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Implement Stack using Two Queues
Spring Boot Change Context Path
Java Program to Construct an Expression Tree for an Infix Expression
Running Spring Boot Applications With Minikube
Java Program to Implement Lloyd’s Algorithm
Các nguyên lý thiết kế hướng đối tượng – SOLID
Integer Constant Pool trong Java
Guava – Join and Split Collections
Getting the Size of an Iterable in Java
Java Program to Implement Repeated Squaring Algorithm
Java Program to Implement JobStateReasons API
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Interface trong Java 8 – Default method và Static method
Java Program to Create a Random Graph Using Random Edge Generation
Lấy ngày giờ hiện tại trong Java
Vector trong Java
Java Program to Perform Addition Operation Using Bitwise Operators
The Modulo Operator in Java
Jackson Ignore Properties on Marshalling