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:
Vector trong Java
Dynamic Proxies in Java
Java Program to Implement Network Flow Problem
Check If a String Is Numeric in Java
Java Program to Implement IdentityHashMap API
Spring Security – security none, filters none, access permitAll
Java Program to Implement Queue using Two Stacks
Spring Boot: Customize Whitelabel Error Page
Exception Handling in Java
Spring MVC + Thymeleaf 3.0: New Features
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Send an email using the SMTP protocol
Java – Write a Reader to File
Java Program to Implement Levenshtein Distance Computing Algorithm
Java Program to Implement ArrayList API
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Implement Shell Sort
Simple Single Sign-On with Spring Security OAuth2
Using Spring @ResponseStatus to Set HTTP Status Code
Java Program to Implement Fermat Primality Test Algorithm
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Copy Constructor
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Implement Self organizing List
Spring RequestMapping
Xử lý ngoại lệ trong Java (Exception Handling)
Chương trình Java đầu tiên
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Lớp LinkedHashMap trong Java
Java Program to find the number of occurrences of a given number using Binary Search approach
Các kiểu dữ liệu trong java
Java Program to Implement Maximum Length Chain of Pairs