72

Possible Duplicate:
Can we convert a byte array into an InputStream in Java?

There is a way to convert an array of bytes (byte[]) to InputStream in Java? I looked at some methods in Apache Commons IO, but found nothing.

Diego
  • 17,399
  • 5
  • 57
  • 65
Renato Dinhani
  • 32,560
  • 53
  • 130
  • 195
  • 13
    ehm, google 'byte array inputstream'? – The Nail Mar 27 '12 at 20:50
  • 3
    asking the ans here might be more faster than google i guess. ;) – Venki Mar 27 '12 at 20:54
  • Yes, I Googled and found it in a page before here. I was thinking was not a simple thing because I'm not much familiar with InputStream "family" – Renato Dinhani Mar 27 '12 at 20:55
  • @TheNail. That string + java search [result](https://www.google.co.il/webhp?sourceid=chrome-instant&ix=sea&ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=byte%20array%20inputstream%20java&oq=&aq=&aqi=&aql=&gs_l=&pbx=1&fp=985957d8efd6e7f9&ix=sea&ion=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&biw=1280&bih=709) – gdoron is supporting Monica Mar 27 '12 at 20:55
  • @Morpheus Yes, it is. Maybe is because this that this website has millions of users. – Renato Dinhani Mar 27 '12 at 20:56
  • 1
    When you post a question, it gives you other questions with similar titles... I find it hard to believe that the question @TheNail posted (or one identical) wasn't listed – ggrigery Mar 27 '12 at 21:15
  • Maybe you want a [ByteArrayInputStream](http://docs.oracle.com/javase/6/docs/api/java/io/ByteArrayInputStream.html)? – Danny Mar 27 '12 at 20:49

6 Answers6

172

ByteArrayInputStream extends InputStream:

InputStream myInputStream = new ByteArrayInputStream(myBytes); 
Diego
  • 17,399
  • 5
  • 57
  • 65
19

Should be easy to find in the javadocs...

byte[] byteArr = new byte[] { 0xC, 0xA, 0xF, 0xE };
InputStream is = new ByteArrayInputStream(byteArr);
david
  • 794
  • 5
  • 22
7

ByteArrayInputStream

Jakub Zaverka
  • 8,664
  • 3
  • 29
  • 47
5

Check out java.io.ByteArrayInputStream

ControlAltDel
  • 32,042
  • 9
  • 48
  • 75
5

Theres the ByteArrayInputStream.

It does exactly this.

Will Hartung
  • 111,665
  • 19
  • 124
  • 199
4

How about ByteArrayInputStream?

Matthew Farwell
  • 59,707
  • 18
  • 122
  • 168