1

Here's my code:

class Copy extends SwingWorker<Void, Void> {

private File selectedfile = new File("D:/Adatok/proba.file");
private File chosenDestination = new File("D:/Adatok/ide/proba.file");

@Override
protected Void doInBackground() throws Exception {
try {
     FileInputStream fileInputStream = new FileInputStream(
     selectedfile);
     BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
     ProgressMonitorInputStream progressMonitorInputStream;
     progressMonitorInputStream = new ProgressMonitorInputStream(Panel.this,"Copying...", bufferedInputStream);
     File outputFile = new File("" + chosenDestination);
     FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
    int data;
    byte[] buffer = new byte[1024];
    while ((data = progressMonitorInputStream.read(buffer)) > 0) {
         bufferedOutputStream.write(buffer);
    }
    bufferedOutputStream.close();
    progressMonitorInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

@Override
public void done() {
     JOptionPane.showMessageDialog(Panel.this, "Ready!", "Done", 1);
}
}

}

It works fine with smaller files, but if I try it with a 3GB file, the progressbar shows wrong progress. When it's 100% the copying isn't finished, in the remaining time the progress bar is set to 0% and doesn't move. What's wrong with it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Roberto
  • 167
  • 1
  • 3
  • 19
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/) though note that even from the snippet I am getting the impression 'bug'. Make sure to `println()` the system properties for [`java.vendor` & `java.version`](http://pscode.org/prop/?prop=java.vendor%2Cjava.version&format=TSV). Also check The [`File.length()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#length%28%29) as understood by Java agrees with your expectation. – Andrew Thompson Jan 23 '13 at 12:45
  • You need to `setProgress()`, like [here](http://stackoverflow.com/a/13538075/261156). – Catalina Island Jan 23 '13 at 12:49
  • 2
    3Gb is greater than 2147483647 (2^31 - 1) which means that you are overflowing (exceeding `Integer.MAX_INT_VALUE`). – Guillaume Polet Jan 23 '13 at 13:01
  • So, what tpye I need to use? And where? – Roberto Jan 23 '13 at 13:25
  • I found this: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6445283 Any solution? – Roberto Jan 23 '13 at 13:33
  • @Roberto simply write your own equivalent classes but supporting `long` values and rely on `File.length()` to compute the accomplished percentage. – Guillaume Polet Jan 23 '13 at 13:58

1 Answers1

1

I know that this is old but I have found this thread more than once over the years while having to fix this same problem. It is a bug in Java as @Roberto has pointed out. I did a similar workaround that the bug reporter posts about.

  1. I copied the source of the ProgessMonitorInputStream and create a new class called ProgessMonitorInputStreamLongBased to avoid confusion
  2. I check the size of the file in bytes and see if it is bigger than Integer.MAX_VALUE:
    • If it is less then no change.
    • If greater then max size is the max int value and scaling will be performed.
  3. The scale is found once and it is equal to the previously found "size" divided by the real length of the file.

I hope this helps someone else who runs across this problem.

lemming622
  • 131
  • 2
  • 12