Coverage Report - com.buckosoft.PicMan.business.util.CopyFile
 
Classes in this File Line Coverage Branch Coverage Complexity
CopyFile
0%
0/53
0%
0/20
7.5
 
 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  0
 public class CopyFile {
 28  
 
 29  0
         private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
 30  
         public static void makeBackup(PicManFacade pmf, File file) {
 31  0
                         String parent = file.getParent();
 32  0
                         parent += ("/save");
 33  0
                         File saveDir = new File(parent);
 34  0
                         if (!saveDir.exists()) {
 35  0
                                 if (!saveDir.mkdir()) {
 36  0
                                         Exception e1 = new Exception("Can't create '" + parent + "'");
 37  0
                                         pmf.addError(e1);
 38  0
                                         return;
 39  
                                 }
 40  
                         }
 41  0
                         if (!saveDir.isDirectory()) {
 42  0
                                 Exception e1 = new Exception("backup dir exists and is not a dir '" + parent + "'");
 43  0
                                 pmf.addError(e1);
 44  0
                                 return;
 45  
                         }
 46  0
                         File backupFile = new File(saveDir, file.getName());
 47  0
                         if (backupFile.exists()) {
 48  0
                                 StringBuffer sb = new StringBuffer();
 49  0
                                 sb.append(file.getName().substring(0, file.getName().length() - 4));
 50  0
                                 sb.append("_");
 51  0
                                 Date d = new Date(backupFile.lastModified());
 52  0
                                 String t = sdf.format(d);
 53  0
                                 int dd = 99999999 - Integer.parseInt(t);
 54  0
                                 sb.append("" + dd);
 55  0
                                 sb.append(".jpg");
 56  0
                                 File backupFileRenamed = new File(saveDir, sb.toString());
 57  0
                                 if (!backupFile.renameTo(backupFileRenamed)) {
 58  0
                                         String es = "Failed to rename backup from '" + file.getName() + "' to '" + sb.toString() + "'";
 59  0
                                         Exception e2 = new Exception(es);
 60  
                                         //logger.error(e2);
 61  0
                                         pmf.addError(e2);
 62  
                                         //return;
 63  
                                 }
 64  
                         }
 65  
                         try {
 66  0
                                 CopyFile.copyFile(file, backupFile, null);
 67  0
                         } catch (IOException e) {
 68  0
                                 Exception e3 = new Exception("Failed to copy file '" + file.getName() + "' to '" + backupFile.getName() + "'", e);
 69  0
                                 pmf.addError(e3);
 70  0
                         }
 71  
                 
 72  0
         }
 73  
         public static void copyFile(File source, File dest, byte[] buf) throws IOException {
 74  0
                 if(!dest.exists()) {
 75  0
                         dest.createNewFile();
 76  
                 }
 77  0
                 if (buf == null)
 78  0
                         buf = new byte[4096];
 79  0
                 InputStream in = null;
 80  0
                 OutputStream out = null;
 81  
                 try {
 82  0
                         in = new FileInputStream(source);
 83  0
                         out = new FileOutputStream(dest);
 84  
 
 85  
                         // Transfer bytes from in to out
 86  
                         int len;
 87  0
                         while ((len = in.read(buf)) > 0) {
 88  0
                                 out.write(buf, 0, len);
 89  
                         }
 90  0
                         in.close();
 91  0
                         out.close();
 92  0
                         dest.setLastModified(source.lastModified());
 93  
                         
 94  
                 }
 95  
                 finally {
 96  0
                         if(in != null) {
 97  0
                                 in.close();
 98  
                         }
 99  0
                         if(out != null) {
 100  0
                                 out.close();
 101  
                         }
 102  
                 }
 103  0
         }        
 104  
         
 105  
 }