Package de.kout.wlFxp

Source Code of de.kout.wlFxp.CopyFile

/**
* Copyright (C) 2003 Alexander Kout
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package de.kout.wlFxp;

import java.nio.*;
import java.nio.channels.*;
import java.io.*;
import java.util.*;

import de.kout.wlFxp.ftp.FtpFile;
import de.kout.wlFxp.ftp.Transfer;

/**
*  this class can copy files and dirs locally
* as of yet no status while copying
*
*@author     Alexander Kout
*@created    3. August 2003
*/
public class CopyFile extends Thread {

  // input vector for commands
  Vector cmd;
  wlFrame frame;
 
  // restart point for resuming
  public volatile long rest;


  /**
   *  Constructor for the CopyFile object
   *
   *@param  frame  the MainFrame
   */
  public CopyFile(wlFrame frame) {
    super();
    this.frame = frame;
    cmd = new Vector();
    start();
  }


  /**
   *  Main processing method for the CopyFile object
   */
  public void run() {
    String command;
    while (true) {
      synchronized (cmd) {
        if (cmd.isEmpty()) {
          try {
            cmd.wait();
          } catch (InterruptedException e) {
          }
        }

        command = (String) cmd.firstElement();
        cmd.removeElementAt(0);
      }
      // copy file to dir
      if (command.equals("cftd")) {
        wlPanel panel = (wlPanel) cmd.firstElement();
        cmd.removeElementAt(0);
        try {
          Transfer transfer = (Transfer) cmd.firstElement();
          cmd.removeElementAt(0);
          FtpFile file = transfer.getSource();
          FtpFile to = transfer.getDest();
          // if dir add files in dir to queue
          if (file.isDirectory()) {
            new File(to.getAbsolutePath()).mkdir();
            frame.getQueueList().removeFirst();
            frame.getQueueList().updateView();
            FtpFile[] files = file.list();
            for (int i = files.length - 1; i >= 0; i--) {
              FtpFile tmp = new FtpFile(files[i].getName());
              tmp.setFtpMode(false);
              tmp.setAbsolutePath(to.getAbsolutePath() + File.separator + files[i].getName());
              frame.getQueueList().addAtBegin(new Transfer(files[i], tmp, transfer.modeFrom, transfer.modeTo, transfer.from_to, null, null));
            }
          // copy the file
          } else {
            copyFileToDir(file, to, panel);
            frame.getQueueList().removeFirst();
            frame.getQueueList().updateView();
          }

        } catch (IOException e) {
          System.out.println(e.toString());
        }
        // wait for TransferManager
        while (!panel.getFrame().getTm().waiting) {
          try {
            Thread.sleep(20);
          } catch (InterruptedException e) {}
        }
        synchronized (panel.getFrame().getTm().done) {
          panel.getFrame().getTm().done.notify();
        }
      }
      try {
        sleep(50);
      } catch (InterruptedException e) {
      }
    }

  }


  /**
   *  this method notifies the thread
   *
   *@param  panel     the parent panel
   *@param  transfer  the transfer object
   */
  public void copy(wlPanel panel, Transfer transfer) {
    synchronized (cmd) {
      cmd.addElement("cftd");
      cmd.addElement(panel);
      cmd.addElement(transfer);
      cmd.notify();
    }
  }


  /**
   *  this method copies a single file to a dir
   *
   *@param  file             the file to copy
   *@param  to               the destination file
   *@param  panel            the destination Panel
   *@exception  IOException  IOException
   */
  private void copyFileToDir(FtpFile file, FtpFile to, wlPanel panel) throws IOException {
    Utilities.print("started copying " + file.getAbsolutePath() + "\n");
    // Open the file and then get a channel from the stream
    FileOutputStream fos = new FileOutputStream(new File(to.getAbsolutePath()));
    FileChannel foc = fos.getChannel();
    FileInputStream fis = new FileInputStream(new File(file.getAbsolutePath()));
    FileChannel fic = fis.getChannel();
    Date d1 = new Date();
    long amount = foc.transferFrom(fic, rest, fic.size()-rest);
    fic.close();
    foc.force(false);
    foc.close();
    Date d2 = new Date();
    long time = d2.getTime() - d1.getTime();
    double secs = time / 1000.0;
    double rate = amount / secs;
    frame.getStatusArea().append(secs + "s " +
        "amount: " + Utilities.humanReadable(amount) +
        " rate: " + Utilities.humanReadable(rate) +
        "/s\n", "black");
    panel.updateView();
  }
}
TOP

Related Classes of de.kout.wlFxp.CopyFile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.