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:
HandlerAdapters in Spring MVC
Use Liquibase to Safely Evolve Your Database Schema
Guide to Selenium with JUnit / TestNG
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Java Program to Implement Hash Tables
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Create a Random Graph Using Random Edge Generation
Generating Random Dates in Java
Collection trong java
Java Program to Implement Direct Addressing Tables
Java Program to Generate Date Between Given Range
Request Method Not Supported (405) in Spring
Java Program to Compute the Area of a Triangle Using Determinants
Using the Not Operator in If Conditions in Java
A Quick JUnit vs TestNG Comparison
Java Program to Implement Pagoda
Handling Errors in Spring WebFlux
Show Hibernate/JPA SQL Statements from Spring Boot
Hướng dẫn Java Design Pattern – Chain of Responsibility
Mảng (Array) trong Java
OAuth2 Remember Me with Refresh Token
Stack Memory and Heap Space in Java
Java Program to Describe the Representation of Graph using Adjacency List
Java 8 Collectors toMap
CyclicBarrier in Java
Shuffling Collections In Java
Java Program to Implement the Monoalphabetic Cypher
Java 8 and Infinite Streams
How to Return 404 with Spring WebFlux
Integer Constant Pool trong Java
Spring Security Authentication Provider
RegEx for matching Date Pattern in Java