Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.FileFilter;
  3. import java.io.IOException;
  4. import java.util.HashSet;
  5. import java.util.TimerTask;
  6.  
  7. import java.util.logging.*;
  8.  
  9. public class Backup implements Runnable{
  10.     Filter filter;
  11.     File originDir;
  12.     File destDir;
  13.    
  14.     //Logger log;
  15.    
  16.     boolean running = false;
  17.     boolean deleteAtTarget;
  18.    
  19.     //Begin constuctors ==========================================================
  20.     public Backup(File oDir, File dDir){
  21.         this(oDir, dDir, null);
  22.     }
  23.    
  24.     public Backup(File oDir, File dDir, Filter filter){
  25.         this(oDir, dDir, filter, false);
  26.     }
  27.    
  28.     public Backup(File oDir, File dDir, Filter filter, boolean deleteAtTarget){
  29.         this(oDir, dDir, filter, deleteAtTarget, null);
  30.     }
  31.    
  32.     public Backup(File oDir, File dDir, Filter filter,
  33.             boolean deleteAtTarget, Logger logger){
  34.        
  35.         if (!oDir.isDirectory() || !dDir.isDirectory()){
  36.             throw new IllegalArgumentException("Supplied paths are not directories.");
  37.         }
  38.        
  39.         //this.log = logger;
  40.         this.originDir = oDir;
  41.         this.destDir = dDir;
  42.         this.filter = filter;
  43.         this.deleteAtTarget = deleteAtTarget;
  44.     }
  45.     // end constructors ===========================================================
  46.    
  47.     public boolean isRunning(){
  48.         return running;
  49.     }
  50.    
  51.     public TimerTask createTimerTask(){
  52.         return new TimerTask(){
  53.             public void run() { run(); }
  54.         };
  55.     }
  56.    
  57.     public void run() {
  58.         synchronized (this){
  59.             if (running) {
  60.                 return;
  61.             }
  62.             running = true;
  63.         }
  64.         doBackup();
  65.         running = false;
  66.     }
  67.    
  68.     private void doBackup() {
  69.         FileWalk fs = new FileWalk(originDir, filter);
  70.         File[] list = fs.getBFSList();  //TODO: use file walk
  71.         if (list == null){
  72.             System.out.println("Files not retrieveable.");
  73.             return;
  74.         }
  75.        
  76.         HashSet<File> delList = new HashSet<File>();
  77.        
  78.         //TODO: use filter
  79.         if (deleteAtTarget){
  80.             for (File f : destDir.listFiles((FileFilter) null)){
  81.                 delList.add(f);
  82.             }
  83.         }
  84.        
  85.         for (File file : list){
  86.             try {
  87.                 File dest = new File (destDir, getRelPath(file, originDir));
  88.                
  89.                 if (deleteAtTarget){
  90.                     delList.remove(dest);
  91.                 }
  92.                
  93.                 if (file.isDirectory()){
  94.                     if(!dest.exists()){
  95.                         dest.mkdirs();
  96.                     }
  97.                 } else {
  98.                     if(dest.exists() && dest.lastModified() >= file.lastModified()
  99.                             && dest.length() == file.length()){
  100.                         System.out.println("File exists, not copied.");
  101.                     } else {
  102.                         dest.getParentFile().mkdirs();
  103.                         copyTo(file, dest);  //TODO: implement copyTo
  104.                     }
  105.                 }
  106.             } catch (IOException ex) {
  107.                 System.out.println("IO Exception");
  108.             }
  109.         }
  110.        
  111.         if (deleteAtTarget){
  112.             //TODO: deleteAtTarget 
  113.         }
  114.     }
  115.    
  116.     public void cancel(){
  117.         return;
  118.         //TODO monitor.disable();
  119.     }
  120.    
  121.     public boolean getDeleteAtTarget(){
  122.         return deleteAtTarget;
  123.     }
  124.    
  125.     public synchronized void setOriginDir (File oDir)
  126.         throws IllegalStateException {
  127.             if (running){
  128.                 throw new IllegalStateException("Backup running");
  129.             }
  130.             if(!oDir.isDirectory()){
  131.                 throw new IllegalArgumentException
  132.                     ("not a directory: " +oDir);
  133.             }
  134.             this.originDir = new File (oDir.getAbsolutePath());
  135.     }
  136.    
  137.     public synchronized void setDestDir (File dDir)
  138.         throws IllegalStateException {
  139.             if (running){
  140.                 throw new IllegalStateException("Backup running");
  141.             }
  142.             if(!dDir.isDirectory()){
  143.                 throw new IllegalArgumentException
  144.                     ("not a directory: " + dDir);
  145.             }
  146.             this.destDir = new File (dDir.getAbsolutePath());
  147.     }
  148.    
  149.     public File getOriginDir(){
  150.         return originDir;
  151.     }
  152.    
  153.     public File getDestDir(){
  154.         return destDir;
  155.     }
  156.    
  157.     //copies data from src to dest
  158.     public void copyTo (File src, File dest) throws IOException{
  159.         try{
  160.           InputStream in = new FileInputStream(src);
  161.          
  162.           //For Append the file.
  163. //        OutputStream out = new FileOutputStream(f2,true);
  164.  
  165.           //For Overwrite the file.
  166.           OutputStream out = new FileOutputStream(dest);
  167.  
  168.           byte[] buf = new byte[1024];
  169.           int len;
  170.           while ((len = in.read(buf)) > 0){
  171.             out.write(buf, 0, len);
  172.           }
  173.           in.close();
  174.           out.close();
  175.           System.out.println("File copied.");
  176.         }
  177.         catch(FileNotFoundException ex){
  178.           System.out.println(ex.getMessage() + " in the specified directory.");
  179.           System.exit(0);
  180.         }
  181.         catch(IOException e){
  182.           System.out.println(e.getMessage());      
  183.         }
  184.      
  185.     }
  186.    
  187.    
  188.     //returns the path of f relative to the given root directory
  189.     public String getRelPath (File f, File root){
  190.         String p = f.getAbsolutePath();
  191.         return p.replace(f.getParent(), "");
  192.     }
  193.    
  194.     //might implement main method
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement