Can I read an entire file with a single read in java on android? -


on android when reading input stream represents file on internet if request read of entire length of file there reason why byte array should partially filled after call?

the read(byte[]) method on java streams may read less bytes requested.

by file on internet assume mean contents of http url.

some http responses not give content-length in advance doesn't makes possible pre-allocate byte array of right size seem doing.

you may interested know apache http client library available on android provides helper method entityutils.tobytearray() fetches content of url , returns sized byte array.

here's piece of code using it:

public byte[] fetchurl(string url) throws ioexception, clientprotocolexception {       httpclient httpclient = new defaulthttpclient();       httpget httpget = new httpget(url);       httpresponse response = httpclient.execute(httpget);       httpentity entity = response.getentity();       return entityutils.tobytearray(entity); } 

note reading whole response in memory not best solution. may preferable report download progress user or parse large document incrementally. in fact response may not fit in ram (e.g large video stream).


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -