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:
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Program to Implement HashTable API
Custom Thread Pools In Java 8 Parallel Streams
Wiring in Spring: @Autowired, @Resource and @Inject
CyclicBarrier in Java
Automatic Property Expansion with Spring Boot
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Implement Segment Tree
Introduction to Using FreeMarker in Spring MVC
Java Program to Implement Stein GCD Algorithm
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Guide to CopyOnWriteArrayList
Java Program to Implement Hopcroft Algorithm
What is Thread-Safety and How to Achieve it?
Java program to Implement Tree Set
OAuth 2.0 Resource Server With Spring Security 5
Java Byte Array to InputStream
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Lập trình mạng với java
How to Read a Large File Efficiently with Java
Send an email with an attachment
Custom Cascading in Spring Data MongoDB
An Introduction to Java.util.Hashtable Class
Java Program to implement Bit Set
Hướng dẫn Java Design Pattern – Interpreter
Spring Boot - Application Properties
Jackson Ignore Properties on Marshalling
Java Program to Implement Ternary Search Algorithm
Java Program to Implement Depth-limited Search
Using Spring ResponseEntity to Manipulate the HTTP Response
Java Program to Implement CopyOnWriteArrayList API
Java Program to Perform Naive String Matching