20 Feb

from the page above:

you need to read each byte from your InputStream and write it to a ByteArrayOutputStream. You can then retrieve the underlying byte array by calling toByteArray(); e.g.

InputStream is =...ByteArrayOutputStream buffer =newByteArrayOutputStream();int nRead;byte[] data =newbyte[16384];while((nRead =,0, data.length))!=-1){
  buffer.write(data,0, nRead);}

buffer.flush();return buffer.toByteArray();


oppure :

Input Stream is ...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int next =;
while (next > -1) {
    next =;
byte[] result = bos.toByteArray();



public static byte[] getBytesFromInputStream(InputStream is) {

try (ByteArrayOutputStream os = new ByteArrayOutputStream();) {

byte[] buffer = new byte[0xFFFF];

for (int len; (len = != -1;) os.write(buffer, 0, len);


return os.toByteArray(); }

catch (IOException e) { return null; }


oppure:questo sembra sia piu giusto…..

ByteArrayOutputStream out =newByteArrayOutputStream();

byte[] buffer =newbyte[1024];


int r =;

if(r ==-1)break;

out.write(buffer,0, r);


byte[] ret = out.toByteArray();

 --------------------- ----



Java IO: InputStream


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: