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:
Convert Character Array to String in Java
Java Program to Implement Binary Search Tree
Java Program to Find Number of Articulation points in a Graph
Spring Data MongoDB Transactions
Java Program to Perform Left Rotation on a Binary Search Tree
Java Deep Learning Essentials - Yusuke Sugomori
Tránh lỗi NullPointerException trong Java như thế nào?
How to Set TLS Version in Apache HttpClient
Spring Boot Configuration with Jasypt
An Example of Load Balancing with Zuul and Eureka
Calling Stored Procedures from Spring Data JPA Repositories
Spring Boot - Admin Server
Spring Boot - File Handling
Java Program to Implement Pollard Rho Algorithm
Java Program to Perform Polygon Containment Test
Java Program to Perform Quick Sort on Large Number of Elements
Intersection of Two Lists in Java
Guide to the Fork/Join Framework in Java
Check if there is mail waiting
Check If a File or Directory Exists in Java
Java Program to Implement Attribute API
Java Program to Implement Binomial Tree
Phương thức forEach() trong java 8
Guide to DelayQueue
Java Program to Represent Linear Equations in Matrix Form
Java Program to Implement Pagoda
Java Program to Implement Suffix Tree
Java Program to Find kth Largest Element in a Sequence
Introduction to Spring MVC HandlerInterceptor
Java Program to Implement Word Wrap Problem
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Spring REST API + OAuth2 + Angular