Java Byte Array to InputStream

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 Baeldung.

2. Convert Using Java

 

First – let's look at the Java solution:

1
2
3
4
5
6
@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:

1
2
3
4
5
6
@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.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

[출처] https://www.baeldung.com/convert-byte-array-to-input-stream

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
» Java Byte Array to InputStream 졸리운_곰 2019.10.24 176
100 [JAXB] JAXB Annotation 설명~ Part. 2 졸리운_곰 2019.10.24 305
99 [JAXB] JAXB Annotation 설명~ Part. 1 졸리운_곰 2019.10.24 136
98 JAXB (3) - marshal, unmarshal에 대해 알아보자. file 졸리운_곰 2019.10.24 142
97 JAXB (2) - XML Schema를 Java Interfaces & Classes로 Binding Compile 하기 file 졸리운_곰 2019.10.24 144
96 JAXB (1) - JAXB 란? file 졸리운_곰 2019.10.24 188
95 XStream으로 자바 객체를 XML로 직렬화하기 file 졸리운_곰 2019.10.15 248
94 [번역] XStream 배우기 : 2분만에 배우는 XStream 졸리운_곰 2019.10.15 232
93 java XML Parser 정리 졸리운_곰 2019.10.05 175
92 [JPA] 쿼리메서드 : 쿼리 연습 조회(findBy..) , 페이징처리 졸리운_곰 2019.03.24 1776
91 스프링 데이터 JPA 레퍼런스 번역 file 졸리운_곰 2019.03.24 1142
90 JPA 개념, class05 JPA 환경설정 졸리운_곰 2019.03.24 209
89 [자바코드] 고유값인 UUID, GUID 생성하기 졸리운_곰 2019.02.27 377
88 [JPA] 복합키 졸리운_곰 2019.02.26 197
87 jpa muli row select result is same row repeat Java 자바 Jpa에서 멀티 로우 반환시 같은값이 반복 file 졸리운_곰 2019.01.01 297
86 [자바] 리스트를 순회하는 방법 졸리운_곰 2018.12.31 277
85 SpringBoot JPA 예제 졸리운_곰 2018.12.31 230
84 SpringBoot JPA 예제(1:N, 양방향) 졸리운_곰 2018.12.31 187
83 SpringBoot JPA 예제(결합 인덱스) 졸리운_곰 2018.12.31 234
82 SpringBoot JPA 예제(@ManyToOne, 단방향) 졸리운_곰 2018.12.31 300
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED