2

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • The only reason the byte array is being refilled is because you are making read calls in a while loop, which doesn't break until the entire file has been read. – Joel Aug 07 '13 at 14:27
  • _Reads up to b.length bytes of data from this input stream into an array of bytes_ That means it populates the given `byte[]`. – Sotirios Delimanolis Aug 07 '13 at 14:29
  • @SotiriosDelimanolis Yes,It reads ,After it should refill right?? Correct me If I'm wrong..new to streams. – Suresh Atta Aug 07 '13 at 14:31
  • You probably want to read this [question](http://stackoverflow.com/questions/7446909/does-fileinputstream-have-an-internal-buffer). The **question**. – devnull Aug 07 '13 at 14:31
  • Every time `while` loops, your `byte[]` will contain the next bytes in the stream. – Sotirios Delimanolis Aug 07 '13 at 14:33

2 Answers2

1

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.

Aurand
  • 5,487
  • 1
  • 25
  • 35
1

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.

Tala
  • 8,888
  • 5
  • 34
  • 38