Table of Contents
1. Overview
In this quick tutorial we’re going to illustrate how to convert a simple byte[] to an InputStream, first using plain java and then the Guava library.
This article is part of the “Java – Back to Basic” series here on VietMX’s Blog.
2. Convert Using Java
First – let’s look at the Java solution:
@Test
public void givenUsingPlainJava_whenConvertingByteArrayToInputStream_thenCorrect()
throws IOException {
byte[] initialArray = { 0, 1, 2 };
InputStream targetStream = new ByteArrayInputStream(initialArray);
}
3. Convert Using Guava
Next – let’s use wrap the byte array into the Guava ByteSource – which then allows us to get the stream:
@Test
public void givenUsingGuava_whenConvertingByteArrayToInputStream_thenCorrect()
throws IOException {
byte[] initialArray = { 0, 1, 2 };
InputStream targetStream = ByteSource.wrap(initialArray).openStream();
}
And there you have it – a simple way of opening an InputStream from a byte array.
Related posts:
Extract links from an HTML page
Java Program to Implement K Way Merge Algorithm
Đồng bộ hóa các luồng trong Java
Java – Write to File
Object cloning trong java
Java Program to Check the Connectivity of Graph Using DFS
Java Program to Find the Vertex Connectivity of a Graph
Giới thiệu Google Guice – Injection, Scope
Java Program to Implement Pagoda
OAuth2 Remember Me with Refresh Token
Java Program to Perform Cryptography Using Transposition Technique
JUnit 5 @Test Annotation
Java String to InputStream
Guide to Apache Commons CircularFifoQueue
Java Program to Implement the Program Used in grep/egrep/fgrep
Performance Difference Between save() and saveAll() in Spring Data
So sánh ArrayList và Vector trong Java
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Spring Boot - Zuul Proxy Server and Routing
Spring Boot - Apache Kafka
Jackson – Unmarshall to Collection/Array
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Implement Gabow Algorithm
Java Program to Represent Graph Using 2D Arrays
Spring Webflux with Kotlin
Spring Boot - Enabling Swagger2
Documenting a Spring REST API Using OpenAPI 3.0
An Example of Load Balancing with Zuul and Eureka
Immutable Map Implementations in Java
Display Auto-Configuration Report in Spring Boot
Marker Interface trong Java
Show Hibernate/JPA SQL Statements from Spring Boot