java - Byte [] used as a BufferInputStream ok but -


ok know buffer array of byte, have never seen following declaration (taken here)

urlconnection con = new url("http://maps...").openconnection(); inputstream = con.getinputstream(); byte bytes[] = new byte[con.getcontentlength()]; is.read(bytes); 

is right way avoid using bufferinputstream object? here have unbuffered stream reading byte []? should not other way around? in advance.

no, not right way. method read() reads n bytes n length of array. can read less bytes (even 0) if no more byte available. number of bytes have been read returned method read(). when end of stream reached method returns -1.

therefore right way read bytes in loop:

byte[] buf = new buf[max]; int n = 0; while ((n = stream.read(buf)) >= 0) {     // deal n first bytes buf } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -