简体   繁体   English

ProgressMonitorInputStream-进度栏未显示实际进度

[英]ProgressMonitorInputStream - progress bar doesn't show the real progress

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. 对于较小的文件,它可以正常工作,但是如果我对3GB的文件尝试,进度条将显示错误的进度。 When it's 100% the copying isn't finished, in the remaining time the progress bar is set to 0% and doesn't move. 当达到100%时,复制尚未完成,在剩余时间内,进度条将设置为0% ,并且不会移动。 What's wrong with it? 它出什么问题了?

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. 正如@Roberto所指出的,这是Java中的一个错误 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 我复制了ProgessMonitorInputStream的源代码,并创建了一个名为ProgessMonitorInputStreamLongBased的新类以避免混淆。
  2. I check the size of the file in bytes and see if it is bigger than Integer.MAX_VALUE: 我检查了文件大小(以字节为单位),看它是否大于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. 如果更大,则最大大小为最大int值,并将执行缩放。
  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. 我希望这可以帮助遇到此问题的其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM