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:
Java Program to Implement Sorted Doubly Linked List
How to Replace Many if Statements in Java
Dynamic Proxies in Java
Check If Two Lists are Equal in Java
Java Program to Search for an Element in a Binary Search Tree
Spring Boot - Introduction
Remove the First Element from a List
Java Program to Implement AVL Tree
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Implement RoleList API
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Quick sort
Java Program to Find Nearest Neighbor for Static Data Set
Convert Hex to ASCII in Java
Java Program to implement Bi Directional Map
Enum trong java
Java Program to Implement LinkedHashMap API
Logging in Spring Boot
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Java Program to Represent Graph Using Adjacency List
HTTP Authentification and CGI/Servlet
List Interface trong Java
Java Program to Implement Park-Miller Random Number Generation Algorithm
Lấy ngày giờ hiện tại trong Java
Exploring the Spring 5 WebFlux URL Matching
Transactions with Spring and JPA
Xử lý ngoại lệ trong Java (Exception Handling)
How to use the Spring FactoryBean?
Create a Custom Auto-Configuration with Spring Boot
Java Program to Implement Bit Array
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Implement Gauss Seidel Method