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:
Guide to Spring 5 WebFlux
Create a Custom Exception in Java
A Guide to EnumMap
Guide to Java 8 groupingBy Collector
Ignore Null Fields with Jackson
HashSet trong java
Tổng quan về ngôn ngữ lập trình java
Hướng dẫn Java Design Pattern – Adapter
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Spring Data JPA Delete and Relationships
A Guide to Java HashMap
Java Program to Perform Encoding of a Message Using Matrix Multiplication
How to Get All Dates Between Two Dates?
Database Migrations with Flyway
Vector trong Java
Map Interface trong java
Spring Boot - Admin Client
Spring Boot - Tomcat Port Number
Java Program to Implement Ternary Heap
Java Program to Describe the Representation of Graph using Adjacency Matrix
Marker Interface trong Java
Java Program to Implement the RSA Algorithm
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java Program to Implement Hash Tables with Double Hashing
Java Program to Implement Dijkstra’s Algorithm using Queue
Converting String to Stream of chars
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Create Java Applet to Simulate Any Sorting Technique
Java – Write to File
Java Program to Implement Sorted Vector