Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.io.FileFilter;
- import java.io.IOException;
- import java.util.HashSet;
- import java.util.TimerTask;
- import java.util.logging.*;
- public class Backup implements Runnable{
- Filter filter;
- File originDir;
- File destDir;
- //Logger log;
- boolean running = false;
- boolean deleteAtTarget;
- //Begin constuctors ==========================================================
- public Backup(File oDir, File dDir){
- this(oDir, dDir, null);
- }
- public Backup(File oDir, File dDir, Filter filter){
- this(oDir, dDir, filter, false);
- }
- public Backup(File oDir, File dDir, Filter filter, boolean deleteAtTarget){
- this(oDir, dDir, filter, deleteAtTarget, null);
- }
- public Backup(File oDir, File dDir, Filter filter,
- boolean deleteAtTarget, Logger logger){
- if (!oDir.isDirectory() || !dDir.isDirectory()){
- throw new IllegalArgumentException("Supplied paths are not directories.");
- }
- //this.log = logger;
- this.originDir = oDir;
- this.destDir = dDir;
- this.filter = filter;
- this.deleteAtTarget = deleteAtTarget;
- }
- // end constructors ===========================================================
- public boolean isRunning(){
- return running;
- }
- public TimerTask createTimerTask(){
- return new TimerTask(){
- public void run() { run(); }
- };
- }
- public void run() {
- synchronized (this){
- if (running) {
- return;
- }
- running = true;
- }
- doBackup();
- running = false;
- }
- private void doBackup() {
- FileWalk fs = new FileWalk(originDir, filter);
- File[] list = fs.getBFSList(); //TODO: use file walk
- if (list == null){
- System.out.println("Files not retrieveable.");
- return;
- }
- HashSet<File> delList = new HashSet<File>();
- //TODO: use filter
- if (deleteAtTarget){
- for (File f : destDir.listFiles((FileFilter) null)){
- delList.add(f);
- }
- }
- for (File file : list){
- try {
- File dest = new File (destDir, getRelPath(file, originDir));
- if (deleteAtTarget){
- delList.remove(dest);
- }
- if (file.isDirectory()){
- if(!dest.exists()){
- dest.mkdirs();
- }
- } else {
- if(dest.exists() && dest.lastModified() >= file.lastModified()
- && dest.length() == file.length()){
- System.out.println("File exists, not copied.");
- } else {
- dest.getParentFile().mkdirs();
- copyTo(file, dest); //TODO: implement copyTo
- }
- }
- } catch (IOException ex) {
- System.out.println("IO Exception");
- }
- }
- if (deleteAtTarget){
- //TODO: deleteAtTarget
- }
- }
- public void cancel(){
- return;
- //TODO monitor.disable();
- }
- public boolean getDeleteAtTarget(){
- return deleteAtTarget;
- }
- public synchronized void setOriginDir (File oDir)
- throws IllegalStateException {
- if (running){
- throw new IllegalStateException("Backup running");
- }
- if(!oDir.isDirectory()){
- throw new IllegalArgumentException
- ("not a directory: " +oDir);
- }
- this.originDir = new File (oDir.getAbsolutePath());
- }
- public synchronized void setDestDir (File dDir)
- throws IllegalStateException {
- if (running){
- throw new IllegalStateException("Backup running");
- }
- if(!dDir.isDirectory()){
- throw new IllegalArgumentException
- ("not a directory: " + dDir);
- }
- this.destDir = new File (dDir.getAbsolutePath());
- }
- public File getOriginDir(){
- return originDir;
- }
- public File getDestDir(){
- return destDir;
- }
- //copies data from src to dest
- public void copyTo (File src, File dest) throws IOException{
- try{
- InputStream in = new FileInputStream(src);
- //For Append the file.
- // OutputStream out = new FileOutputStream(f2,true);
- //For Overwrite the file.
- OutputStream out = new FileOutputStream(dest);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0){
- out.write(buf, 0, len);
- }
- in.close();
- out.close();
- System.out.println("File copied.");
- }
- catch(FileNotFoundException ex){
- System.out.println(ex.getMessage() + " in the specified directory.");
- System.exit(0);
- }
- catch(IOException e){
- System.out.println(e.getMessage());
- }
- }
- //returns the path of f relative to the given root directory
- public String getRelPath (File f, File root){
- String p = f.getAbsolutePath();
- return p.replace(f.getParent(), "");
- }
- //might implement main method
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement