0

My program is a file manager and I want to pause a channel to channel copy file when I click on pause button. my copy method

    fcin = new FileInputStream(source.getPath().replace("\\", "/")).getChannel();
    fcout = new FileOutputStream(dest.getPath().replace("\\", "/")).getChannel();
    processValue value = new processValue(bar, source, dest);
    Thread t1 = new Thread(value);
    t1.start();
    this.setVisible(true);
    fcin.transferTo(0, fcin.size(), fcout);
    retVal = true;

and t1 is for a JProgressBar for this process. Can you help me??

Naeem Baghi
  • 811
  • 2
  • 14
  • 29

1 Answers1

1

Change the fcin.size() parameter to some reasonable block size and the 0 to some count then call it in a loop that checks the size of a pause flag, see this question for an example of the block transfer.

If you could add a pause button that toggles its state:

public static void fileCopy(File in, File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {
        // inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows
        // magic number for Windows, (64Mb - 32Kb) DO NOT EXCEED (64 * 1024 * 1024) - (32 * 1024)
        int maxCount = (32 * 1024); // This should allow the code to check the pause state
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            if (not PauseButton.GetState() == PausedState) { // <--- This is the bit you will have to add
                position += inChannel.transferTo(position, maxCount, outChannel);
            }
        }
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}
Community
  • 1
  • 1
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73