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:
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Hướng dẫn Java Design Pattern – Memento
Java Program to Implement RenderingHints API
New Features in Java 11
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Getting Started with Stream Processing with Spring Cloud Data Flow
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Custom Thread Pools In Java 8 Parallel Streams
Từ khóa this và super trong Java
Java 8 Stream findFirst() vs. findAny()
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Implement Hash Trie
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Spring 5 Testing with @EnabledIf Annotation
Consuming RESTful Web Services
Serverless Functions with Spring Cloud Function
Java Program to Implement Interpolation Search Algorithm
How to Kill a Java Thread
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to find the maximum subarray sum using Binary Search approach
Spring Security OAuth Login with WebFlux
Java Program to Search for an Element in a Binary Search Tree
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Limiting Query Results with JPA and Spring Data JPA
Java Program to Implement Threaded Binary Tree
A Custom Media Type for a Spring REST API
Spring WebClient Requests with Parameters
Basic Authentication with the RestTemplate
Adding Parameters to HttpClient Requests