DataOutputStream lWriter = new DataOutputStream(connection.getOutputStream());
int bytesRead = 0;
int offset = 0;
byte[] bytes = new byte[1048576]; // read 1MB at a time
RandomAccessFile f = new RandomAccessFile(xmlFile, "rw");
while(bytesRead != -1){
offset += f.read(bytes, offset, bytes.length);
if (bytesRead != -1){
lWriter.write(bytes, 0, bytes.length);
}
}
With this code I'm getting an index out of bounds exception at f.read(). I'm probably misusing the arguments offset and length incorrectly. Wouldn't each time I read in a chunk, I should move the offset the size of the chunk? Maybe I just need to read less in at time and use a smaller buffer?
Currently I have this implementation working, but I'm worried about memory usage:
DataOutputStream lWriter = new DataOutputStream(connection.getOutputStream());
lWriter.write(fileToBytes(xmlFile));
Thanks for any help!