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 Regular Falsi Algorithm
Java Program to Solve any Linear Equations
Jackson – Change Name of Field
ETL with Spring Cloud Data Flow
The Modulo Operator in Java
A Guide to Java 9 Modularity
String Joiner trong Java 8
Why String is Immutable in Java?
Java – String to Reader
Binary Numbers in Java
Java Program to Implement ArrayDeque API
Spring MVC Setup with Kotlin
Hướng dẫn Java Design Pattern – Null Object
Deploy a Spring Boot App to Azure
Toán tử trong java
Testing an OAuth Secured API with Spring MVC
Spring Security Login Page with React
Java Program to Implement JobStateReasons API
Primitive Type Streams in Java 8
ArrayList trong java
Guide to Java OutputStream
How to Find an Element in a List with Java
Java Program to implement Sparse Vector
Introduction to Spring Data REST
Feign – Tạo ứng dụng Java RESTful Client
How to Return 404 with Spring WebFlux
Concrete Class in Java
Java Program to Implement HashSet API
Java Program to Implement Hash Tables with Double Hashing
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Converting Iterator to List
Java Program to Implement Quick Hull Algorithm to Find Convex Hull