First my apology for lengthy description. But I try my best to make it to the point.
The objective is to group individual files with combined size of 50MB and send multiples of 50MB each. Because I don't want to send a very small file over network individually. besides sending all at one shot won't help either as the size of combined files could be too large.
For (e.g.) if I attach 4 files of size 50MB each then combined size would be 200MB which is too large to send. So instead of sending 200MB at one shot, send only multiples of 50MB one by one, so looping through file_list and measuring each file size if it is <50MB then adding it to the send_list so keep adding individual files until it reaches 50MB, so once it reaches 50MB then send that file_list. Continue iterate over the file_list and do the same for rest of files.
What I have tried:
Assume the file_list contains 4 files i.e. 50MB, 40MB, 30MB, 20MB
Collections.sort(files, new Comparator<File>() {
@Override
public int compare(File fileOne, File fileTwo) {
return fileOne.getSize() - fileTwo.getSize();//Sorting Asc.
}
});
Integer total = 0;
ArrayList<File> files = new ArrayList<File>();
for(File file:files) {
total = total + file.getSize();
if(total <= 51200 ) { //51200KB = 50MB
files.add(file);
if(total == 51200) {
send(files);
files = new ArrayList<File>();
total = 0;
}
} else {
send(files);
files = new ArrayList<File>();
total = 0;
}
}
if(files.size() > 0)
send(files);
But it is missing 30MB file above - what is wrong with this logic?