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 Java Design Pattern – Interpreter
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Find Inverse of a Matrix
Guide to Apache Commons CircularFifoQueue
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement Hash Tables Chaining with List Heads
A Custom Media Type for a Spring REST API
Java Program to Generate Date Between Given Range
Converting a List to String in Java
Introduction to the Java ArrayDeque
Tránh lỗi NullPointerException trong Java như thế nào?
Spring Boot Actuator
Jackson Exceptions – Problems and Solutions
How to Use if/else Logic in Java 8 Streams
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Java Program to Check whether Undirected Graph is Connected using BFS
Configure a Spring Boot Web Application
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Hash Tables
Jackson – Bidirectional Relationships
Spring Data JPA Delete and Relationships
Spring Boot Security Auto-Configuration
Java Program to Implement Queue
Spring Boot - Cloud Configuration Server
Spring Cloud – Tracing Services with Zipkin
Map Serialization and Deserialization with Jackson
File Upload with Spring MVC
Vòng lặp for, while, do-while trong Java
Spring RequestMapping
Từ khóa this và super trong Java
Cài đặt và sử dụng Swagger UI