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 Apache Commons CircularFifoQueue
SOAP Web service: Authentication trong JAX-WS
Java Program to Implement Knapsack Algorithm
Java Program to Implement HashMap API
Integer Constant Pool trong Java
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Giới thiệu Aspect Oriented Programming (AOP)
Xây dựng ứng dụng Client-Server với Socket trong Java
Check if there is mail waiting
Introduction to Spring Cloud Stream
CharSequence vs. String in Java
Hướng dẫn Java Design Pattern – Chain of Responsibility
Using a Spring Cloud App Starter
Java Program to Implement Iterative Deepening
Luồng Daemon (Daemon Thread) trong Java
Spring MVC Tutorial
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Immutable ArrayList in Java
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Spring @RequestMapping New Shortcut Annotations
Java Concurrency Interview Questions and Answers
Java Program to subtract two large numbers using Linked Lists
Marker Interface trong Java
A Guide to Apache Commons Collections CollectionUtils
Summing Numbers with Java Streams
Java Program to Implement Graph Structured Stack
Hashtable trong java
Spring Security – Reset Your Password
Java Deep Learning Essentials - Yusuke Sugomori
Java – Reader to InputStream
Spring 5 Testing with @EnabledIf Annotation