convert-inputStream_into_byte[]-in-java

20 Feb

http://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-in-java

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 = is.read(data,0, data.length))!=-1){
  buffer.write(data,0, nRead);}

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

..........................................

oppure :

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

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

oppure:

public static byte[] getBytesFromInputStream(InputStream is) {

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

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

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

os.flush();

return os.toByteArray(); }

catch (IOException e) { return null; }

}

————————————————–
oppure:questo sembra sia piu giusto…..

ByteArrayOutputStream out =newByteArrayOutputStream();

byte[] buffer =newbyte[1024];

while(true){

int r = in.read(buffer);

if(r ==-1)break;

out.write(buffer,0, r);

}

byte[] ret = out.toByteArray();

---------------------------------------------------------
-
 ---------------------
http://www.techzoo.org/tutorials/servlet-3-file-uplaod-example-using-tomcat-7.html ----

----------------------------------------------------------------------------------- 
ByteArrayInputStream

 http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html 

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

Java IO: InputStream



http://tutorials.jenkov.com/java-io/inputstream.html

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: