java - How the buffer byte array is continuously filling while streaming? -


the below piece of code using reading files

 int bytesread;  byte[] bytes = new byte[1000];  //buffer  fileinputstream fis = new fileinputstream(uploadedfile);      while ((bytesread = fis.read(bytes)) != -1) {           fis.read(bytes, 0, bytesread);     }  fis.close(); 

as per api of read() method

reads b.length bytes of data input stream array of bytes. method blocks until input available.

there no specified refills bytes array,but stream filling array until file read..

but how internally it's maintaining magic done ??

i saw source code or read method

public int more ...read(byte b[]) throws ioexception { 214        return readbytes(b, 0, b.length); 215    } 

and readbytes's source code

200    private native int more ...readbytes                 (byte b[], int off, int len) throws ioexception; 

there noting mentioned how bytes ..

i uploaded 500mb file without problem,with allocation 1000 bytes array.

if you're asking why can read ~500 mb file 1 kb buffer, it's because overwrite contents of buffer each time go through loop (approximately 500,000 times).

if you're asking how read function implemented, notice underlying call includes keyword native. means native code being called via jni. exact implementation going jvm , os dependent.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -