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:
Java Program to Optimize Wire Length in Electrical Circuit
Java toString() Method
Constructor Dependency Injection in Spring
Java Program to implement Array Deque
Cachable Static Assets with Spring MVC
Partition a List in Java
Use Liquibase to Safely Evolve Your Database Schema
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Biểu thức Lambda trong Java 8 – Lambda Expressions
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Rope
New Features in Java 9
Java Program to Create a Balanced Binary Tree of the Incoming Data
A Guide to Java HashMap
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement Self Balancing Binary Search Tree
Remove the First Element from a List
The Difference Between Collection.stream().forEach() and Collection.forEach()
Connect through a Proxy
Jackson vs Gson
Map Serialization and Deserialization with Jackson
Java Program to Implement Max Heap
Zipping Collections in Java
StringBuilder vs StringBuffer in Java
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Double Ended Queue
Java Program to Implement Unrolled Linked List
Java Program to Implement Min Heap
Java Program to Perform Stooge Sort
Hướng dẫn Java Design Pattern – Mediator
Java Program to Implement Rolling Hash
Jackson Unmarshalling JSON with Unknown Properties