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 java.util.concurrent.BlockingQueue
Java Program to Implement Sorted Array
Working with Tree Model Nodes in Jackson
Java 8 Streams peek() API
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Returning Image/Media Data with Spring MVC
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Solve TSP Using Minimum Spanning Trees
How to Set TLS Version in Apache HttpClient
Java Program to Implement ConcurrentLinkedQueue API
Create Java Applet to Simulate Any Sorting Technique
Java Program to Implement Sieve Of Atkin
So sánh ArrayList và LinkedList trong Java
Java Program to Implement vector
Thao tác với tập tin và thư mục trong Java
Java Program to implement Array Deque
Simplify the DAO with Spring and Java Generics
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Implement TreeSet API
Hashtable trong java
Apache Commons Collections SetUtils
Intro to the Jackson ObjectMapper
Giới thiệu Design Patterns
Java Program to Implement Find all Back Edges in a Graph
Java program to Implement Tree Set
Java Program to Implement Heap Sort Using Library Functions
Java Program to Implement D-ary-Heap
Java Program to Solve Tower of Hanoi Problem using Stacks
Hướng dẫn Java Design Pattern – Mediator
“Stream has already been operated upon or closed” Exception in Java
Spring Boot Integration Testing with Embedded MongoDB