I’m implementing a file transfer server, and I’ve run into an issue with sending a file larger than 2 GB over the network. The issue starts when I get the File I want to work with and try to read its contents into a byte[]. I have a for loop :
for(long i = 0; i < fileToSend.length(); i += PACKET_SIZE){
fileBytes = getBytesFromFile(fileToSend, i);
where getBytesFromFile() reads a PACKET_SIZE amount of bytes from fileToSend which is then sent to the client in the for loop. getBytesFromFile() uses i as an offset; however, the offset variable in FileInputStream.read() has to be an int. I’m sure there is a better way to read this file into the array, I just haven’t found it yet.
I would prefer to not use NIO yet, although I will switch to using that in the future. Indulge my madness 🙂
It doesn’t look like you’re reading data from the file properly. When reading data from a stream in Java, it’s standard practice to read data into a buffer. The size of the buffer can be your packet size.
Note that, the size of the buffer array remains constant. But– if the buffer cannot be filled (like when it reaches the end of the file), the remaining elements of the array will contain data from the last packet, so you must ignore these elements (this is what the
out.write()line in my code sample does)