View Javadoc
1   /******************************************************************************
2    * CopyFile.java - Copy a file
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2009 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.business.util;
9   
10  import java.io.File;
11  import java.io.FileInputStream;
12  import java.io.FileOutputStream;
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.io.OutputStream;
16  import java.text.SimpleDateFormat;
17  import java.util.Date;
18  
19  import com.buckosoft.PicMan.business.PicManFacade;
20  
21  /** Copy a file from point a to point b
22   * @author Dick Balaska
23   * @since 2009/07/17
24   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/business/util/CopyFile.java">CopyFile.java</a>
25   * @see com.buckosoft.PicMan.business.mosaic.engine
26   */
27  public class CopyFile {
28  
29  	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
30  	public static void makeBackup(PicManFacade pmf, File file) {
31  			String parent = file.getParent();
32  			parent += ("/save");
33  			File saveDir = new File(parent);
34  			if (!saveDir.exists()) {
35  				if (!saveDir.mkdir()) {
36  					Exception e1 = new Exception("Can't create '" + parent + "'");
37  					pmf.addError(e1);
38  					return;
39  				}
40  			}
41  			if (!saveDir.isDirectory()) {
42  				Exception e1 = new Exception("backup dir exists and is not a dir '" + parent + "'");
43  				pmf.addError(e1);
44  				return;
45  			}
46  			File backupFile = new File(saveDir, file.getName());
47  			if (backupFile.exists()) {
48  				StringBuffer sb = new StringBuffer();
49  				sb.append(file.getName().substring(0, file.getName().length() - 4));
50  				sb.append("_");
51  				Date d = new Date(backupFile.lastModified());
52  				String t = sdf.format(d);
53  				int dd = 99999999 - Integer.parseInt(t);
54  				sb.append("" + dd);
55  				sb.append(".jpg");
56  				File backupFileRenamed = new File(saveDir, sb.toString());
57  				if (!backupFile.renameTo(backupFileRenamed)) {
58  					String es = "Failed to rename backup from '" + file.getName() + "' to '" + sb.toString() + "'";
59  					Exception e2 = new Exception(es);
60  					//logger.error(e2);
61  					pmf.addError(e2);
62  					//return;
63  				}
64  			}
65  			try {
66  				CopyFile.copyFile(file, backupFile, null);
67  			} catch (IOException e) {
68  				Exception e3 = new Exception("Failed to copy file '" + file.getName() + "' to '" + backupFile.getName() + "'", e);
69  				pmf.addError(e3);
70  			}
71  		
72  	}
73  	public static void copyFile(File source, File dest, byte[] buf) throws IOException {
74  		if(!dest.exists()) {
75  			dest.createNewFile();
76  		}
77  		if (buf == null)
78  			buf = new byte[4096];
79  		InputStream in = null;
80  		OutputStream out = null;
81  		try {
82  			in = new FileInputStream(source);
83  			out = new FileOutputStream(dest);
84  
85  			// Transfer bytes from in to out
86  			int len;
87  			while ((len = in.read(buf)) > 0) {
88  				out.write(buf, 0, len);
89  			}
90  			in.close();
91  			out.close();
92  			dest.setLastModified(source.lastModified());
93  			
94  		}
95  		finally {
96  			if(in != null) {
97  				in.close();
98  			}
99  			if(out != null) {
100 				out.close();
101 			}
102 		}
103 	}	
104 	
105 }