How the buffer byte array is continuously filling

2020-02-11 09:47发布

The below piece of code using to reading for 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 up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

There is no where specified that it refills the bytes array,but the stream filling the array until the file successfully read..

But how the internally it's maintaining to get this 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 is

200    private native int More ...readBytes
                (byte b[], int off, int len) throws IOException;

There is noting mentioned that how bytes ..

I uploaded a 500MB file without any problem,with allocation that 1000 bytes array.

2条回答
疯言疯语
2楼-- · 2020-02-11 09:53

A great article on readBytes was published by Michael Schaeffer

In short:

File I/O in Java is implemented by reading into a local buffer, and then copying from the local buffer into the Java byte[] initially passed into int read(byte byf[]). This double-copy is slow, but it also requires heap allocation of a second read buffer, if the read buffer is large than 8K.

Many other helpful details to mention, but it's easier to read it.

查看更多
Lonely孤独者°
3楼-- · 2020-02-11 10:07

If you're asking why you can read a ~500 MB file with a roughly 1 KB buffer, it's because you overwrite the contents of the buffer each time you go through the loop (approximately 500,000 times).

If you're asking how the read function is actually implemented, notice that the underlying call includes the keyword native. That means that native code is being called via JNI. The exact implementation is going to be JVM and OS dependent.

查看更多
登录 后发表回答