Coverage Report - com.buckosoft.PicMan.business.util.MD5SumUpdater
 
Classes in this File Line Coverage Branch Coverage Complexity
MD5SumUpdater
0%
0/40
0%
0/2
1.75
 
 1  
 /**
 2  
  * 
 3  
  */
 4  
 package com.buckosoft.PicMan.business.util;
 5  
 
 6  
 import java.awt.image.BufferedImage;
 7  
 import java.io.ByteArrayOutputStream;
 8  
 import java.util.Date;
 9  
 import java.util.Iterator;
 10  
 import java.util.List;
 11  
 
 12  
 import javax.imageio.ImageIO;
 13  
 import javax.imageio.ImageWriter;
 14  
 import javax.imageio.stream.ImageOutputStream;
 15  
 
 16  
 import org.apache.commons.logging.Log;
 17  
 import org.apache.commons.logging.LogFactory;
 18  
 
 19  
 import com.buckosoft.PicMan.business.PicManFacade;
 20  
 import com.buckosoft.PicMan.domain.JobLogEntry;
 21  
 import com.buckosoft.PicMan.domain.Pic;
 22  
 import com.buckosoft.PicMan.image.PicReader;
 23  
 
 24  
 /**
 25  
  * @author dick
 26  
  *
 27  
  */
 28  0
 public class MD5SumUpdater {
 29  0
         protected final static Log log = LogFactory.getLog(MD5SumUpdater.class);
 30  
 
 31  0
         private        int picProcessing = 0;
 32  
         private        PicManFacade        pmf;
 33  
 
 34  
         /** Set the reference to the PicMan API.
 35  
          * @param pmf The PicManFacade
 36  
          */
 37  
         public        void setPicMan(PicManFacade pmf) {
 38  0
                 this.pmf = pmf;
 39  0
         }
 40  
 
 41  
         /** Which ordinal picture are we working on?
 42  
          * This is just a status indicator
 43  
          * @return the picProcessing
 44  
          */
 45  
         public int getPicProcessing() {
 46  0
                 return picProcessing;
 47  
         }
 48  
 
 49  
         /** Entry point from BatchManager to run the job
 50  
          */
 51  
         public void run() {
 52  0
                 List<Pic> pics = pmf.getDB().getPics();
 53  0
                 Iterator<Pic> iter = pics.iterator();
 54  0
                 int        picCount = pics.size();
 55  0
                 picProcessing = 0;
 56  0
                 JobLogEntry jle = new JobLogEntry();
 57  0
                 jle.setType(JobLogEntry.MD5SUM);
 58  0
                 pmf.addJobToLog(jle);
 59  
                 Pic pic;
 60  0
                 while (iter.hasNext()) {
 61  0
                         picProcessing++;
 62  0
                         int cp = picProcessing * 100 / picCount;
 63  0
                         jle.setName("" + cp + "%");
 64  0
                         pic = iter.next();
 65  0
                         calculateMD5Sum(pmf, pic);
 66  
                         //log.info("md5sum for " + pic.getName() + " = " + pic.getMd5());
 67  0
                         pmf.getDB().updatePic(pic);
 68  0
                 }
 69  0
                 jle.setEndTime(new Date());
 70  0
         }
 71  
         
 72  
         public static void calculateMD5Sum(PicManFacade pmf, Pic pic) {
 73  
         
 74  
                 //FileInputStream fis;
 75  
                 String md5;
 76  
                 //String fullPath = null;
 77  
                 try {
 78  0
                         BufferedImage bi = pmf.getPicReader().readPic(pic);
 79  0
                         ByteArrayOutputStream out = new ByteArrayOutputStream();
 80  0
                         ImageOutputStream ios = ImageIO.createImageOutputStream(out);
 81  0
                         Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
 82  0
                         ImageWriter writer = (ImageWriter)writers.next();
 83  0
                         writer.setOutput(ios);
 84  0
                         writer.write(bi);
 85  0
                         byte[] data = out.toByteArray();
 86  0
                         md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
 87  
                         
 88  
                         //fullPath = PicReader.getFullPath(pmf, pic);
 89  
 //                        fis = new FileInputStream(new File(fullPath));
 90  
 //                        md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
 91  
 //                        fis.close();
 92  0
                 } catch (Exception e) {
 93  
                         // TODO Auto-generated catch block
 94  
                         //e.printStackTrace();
 95  0
                         log.warn("md5 file error on " + PicReader.getFullPath(pmf, pic));
 96  0
                         pic.setMd5(0);
 97  0
                         return;
 98  0
                 }
 99  0
                 long l = Long.parseLong(md5.substring(0, 8), 16);
 100  0
                 pic.setMd5(l);
 101  
                 
 102  
                 //byte[] m = org.apache.commons.codec.digest.DigestUtils.md5(fis);
 103  
                 //long l = m[0] + m[1]<<8 + m[2]<<16 + m[3]<<24 + m[4]<<32 + m[5]<<40 + m[6]<<48 + m[7]<<56;
 104  
 /*                MessageDigest md = null;
 105  
                 try {
 106  
                         md = MessageDigest.getInstance("MD5");
 107  
                 } catch (NoSuchAlgorithmException e) {
 108  
                         log.error(e);
 109  
                         return;
 110  
                 }
 111  
                 InputStream is = null;
 112  
                 try {
 113  
                         is = Files.newInputStream(Paths.get("file.txt"));
 114  
                 } catch (IOException e) {
 115  
                         log.error(e);
 116  
                         return;
 117  
                 }
 118  
                 DigestInputStream dis = new DigestInputStream(is, md);
 119  
                  Read stream to EOF as normal... 
 120  
 
 121  
                 byte[] digest = md.digest();                
 122  0
 */        }
 123  
 }