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:
Understanding Memory Leaks in Java
Create a Custom Exception in Java
Shuffling Collections In Java
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java IO vs NIO
Hướng dẫn Java Design Pattern – Bridge
Java – Reader to Byte Array
How to Get the Last Element of a Stream in Java?
Spring Security Remember Me
Convert char to String in Java
Java Program to Check for balanced parenthesis by using Stacks
Spring Boot - Admin Server
Java Program to Implement Rope
Giới thiệu Aspect Oriented Programming (AOP)
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement K Way Merge Algorithm
Encode/Decode to/from Base64
Getting Started with Forms in Spring MVC
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Perform Sorting Using B-Tree
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Custom Cascading in Spring Data MongoDB
Spring MVC Content Negotiation
Mapping a Dynamic JSON Object with Jackson
Hướng dẫn Java Design Pattern – Transfer Object
Logging in Spring Boot
Java Optional as Return Type
Sử dụng CyclicBarrier trong Java
How to Change the Default Port in Spring Boot
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Java Program to Implement the MD5 Algorithm
New Features in Java 13