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:
Apache Tiles Integration with Spring MVC
So sánh ArrayList và Vector trong Java
Java Program to Implement vector
Spring Security Remember Me
Prevent Cross-Site Scripting (XSS) in a Spring Application
Apache Commons Collections BidiMap
Hướng dẫn sử dụng Printing Service trong Java
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Red Black Tree
Dockerizing a Spring Boot Application
Extract links from an HTML page
RestTemplate Post Request with JSON
Spring WebFlux Filters
A Guide to TreeSet in Java
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Spring Boot - Google OAuth2 Sign-In
Java Program to Implement Double Ended Queue
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Java – Random Long, Float, Integer and Double
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Find Number of Articulation points in a Graph
Introduction to Java 8 Streams
How to Get a Name of a Method Being Executed?
Java Convenience Factory Methods for Collections
Phương thức tham chiếu trong Java 8 – Method References
Guide to Java Instrumentation
Java Scanner hasNext() vs. hasNextLine()
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Streams vs Vavr Streams
Đồng bộ hóa các luồng trong Java