0

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?

grek
  • 93
  • 1
  • 11
  • I wouldn't bother. Your assessment that 200MB is 'too large to send' is entirely arbitrary. I would use a good FTP client with resume facilities for this and not write any code at all. – user207421 Apr 17 '15 at 04:50
  • Correct - that is entirely arbitrary. I am using Spring MVC Portlet and attach & send files asynchronously using AJAX, the files being sent to middle-tier application over web-services, to process. – grek Apr 17 '15 at 04:55
  • Have you implemented this code or is it pseudo code, I find lots of mistakes here comparetor implementation is not proper. You create instance of files and loop through the same instance ArrayList files = new ArrayList(); for(File file:files) – Karthik Prasad Apr 17 '15 at 07:16
  • Keep in mind - check file on retrieval not when it's already retrieved! – Yuri Apr 17 '15 at 10:49

1 Answers1

0

Replace the line

if(total = 51200)

with the following:

if(total == 51200)

total = 51200 is not a comparison statement, it is rather an assignment which is deviating from your logic you intend to implement.

Hope that helps.

Nirmal
  • 1,229
  • 1
  • 15
  • 31