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

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
310 GEF Programmer Guide 번역 졸리운_곰 2019.11.25 227
309 Learn Eclipse GMF in 15 minutes file 졸리운_곰 2019.11.20 224
308 RCP 에디터 정리 졸리운_곰 2019.11.20 333
307 다른 그림과 관련하여 GEF 편집기 레이아웃에서 그림의 위치 제한 조건을 동적으로 계산 Dynamically calculating the position constraints for a figure in a GEF editor layout in relation to another figure file 졸리운_곰 2019.11.20 277
306 추상(abstract) 클래스가 필요한 기본적인 이유 졸리운_곰 2019.10.30 247
» Java Byte Array to InputStream 졸리운_곰 2019.10.24 176
304 [JAXB] JAXB Annotation 설명~ Part. 2 졸리운_곰 2019.10.24 305
303 [JAXB] JAXB Annotation 설명~ Part. 1 졸리운_곰 2019.10.24 136
302 JAXB (3) - marshal, unmarshal에 대해 알아보자. file 졸리운_곰 2019.10.24 142
301 JAXB (2) - XML Schema를 Java Interfaces & Classes로 Binding Compile 하기 file 졸리운_곰 2019.10.24 144
300 JAXB (1) - JAXB 란? file 졸리운_곰 2019.10.24 188
299 XStream으로 자바 객체를 XML로 직렬화하기 file 졸리운_곰 2019.10.15 248
298 [번역] XStream 배우기 : 2분만에 배우는 XStream 졸리운_곰 2019.10.15 232
297 RCP 등에서 .mf 파일로 다른 프로젝트 익스포트 포함시 라이브러리(메소드)를 찾지 못할 때, Eclipse RCP - cant resolve importing libraries in final build file 졸리운_곰 2019.10.15 410
296 java XML Parser 정리 졸리운_곰 2019.10.05 175
295 ESE2006-EclipseModelingSymposium15_GMF.pdf file 졸리운_곰 2019.09.21 302
294 GMF_Creation_Review.pdf file 졸리운_곰 2019.09.21 366
293 Eclipse EMF and GMF Tutorial file 졸리운_곰 2019.09.21 222
292 GMF Tutorial/ko file 졸리운_곰 2019.09.20 347
291 Model Driven Architecture approach to domain of graphical editors file 졸리운_곰 2019.09.20 268
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED