Java read file larger than 2 GB (using Chunking)

I am implementing a file transfer server, and I had a problem sending a file larger than 2 GB in size over the network. The problem starts when I get the Fileone I want to work with and try to read its contents in byte[]. I have a for loop:

for(long i = 0; i < fileToSend.length(); i += PACKET_SIZE){
    fileBytes = getBytesFromFile(fileToSend, i);  


where it getBytesFromFile()reads the number of bytes PACKET_SIZEfrom fileToSend, which is then sent to the client in a for loop. getBytesFromFile()uses ias an offset; however, the bias variable in FileInputStream.read()should be int. I am sure there is a better way to read this file in an array, I have not found it yet.

I would prefer not to use NIO yet, although I will switch to using this in the future. Indulge my madness :-)

+5
source share
2 answers

It doesn't seem like you are reading the data from the file correctly. When reading data from a stream in Java, the standard practice is to read data into a buffer. The buffer size may be the size of your packet.

File fileToSend = //...
InputStream in = new FileInputStream(fileToSend);
OutputStream out = //...
byte buffer[] = new byte[PACKET_SIZE];
int read;
while ((read = in.read(buffer)) != -1){
  out.write(buffer, 0, read);
}
in.close();
out.close();

Note that the size of the buffer array remains constant. But - if the buffer cannot be filled (for example, when it reaches the end of the file), the remaining elements of the array will contain data from the last package, so you should ignore these elements (this is what the line does out.write()in my code example)

+4
source

Umm, understand that your handling of a variable is wrong.

Iteration 0: i=0
Iteration 1: i=PACKET_SIZE
...
...
Iteration n: i=PACKET_SIZE*n
-1
source

All Articles