/* Copyright (c) 2006 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn.v15; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Random; import java.util.Scanner; import cc.glsn.Util; public class ParanoidFileCopy { public static final int BlockSize=128*1024; public static final int BlockCount=20*1024*1024/BlockSize; private PCMonitor In2MD; private PCMonitor MD2Out; private PCMonitor Out2In; /** * @param args */ public static void main(String[] args) { if (args.length !=4 ) { System.out.println("Syntax: ParanoidFileCopy "); System.exit(-1); } String SrcDir=args[0]; String DstDir=args[1]; String FileBase=args[2]; String MD5Path=args[3]; new ParanoidFileCopy(SrcDir,DstDir,FileBase,MD5Path); } public ParanoidFileCopy(String SrcDir, String DstDir, String FileBase, String MD5Path) { In2MD=new PCMonitor(); MD2Out=new PCMonitor(); Out2In=new PCMonitor(); for(int i=0; i0) { Sz+=r; B.Filled+=r; } else { System.out.println("End of input file reached before all bytes read!"); System.exit(-1); } } if (Sz==TotalSz) { B.LastBlock=true; } In2MD.produce(B); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } } } class MDThread extends Thread { public String MD5Sum; public MDThread() { } public void run() { try { MessageDigest SIG=MessageDigest.getInstance("MD5"); boolean LastBlockSeen=false; while(!LastBlockSeen) { Block B=In2MD.consume(); SIG.update(B.Data,0,B.Filled); if (B.LastBlock) LastBlockSeen=true; MD2Out.produce(B); } byte D[]=SIG.digest(); StringBuilder S=new StringBuilder(32); BigInteger BI=new BigInteger(1,D); S.append(BI.toString(16)); while (S.length() < 32) { S.insert(0,'0'); } MD5Sum=S.toString(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } } } class OutputThread extends Thread { File OutFile; public OutputThread(File OutFile) { this.OutFile=OutFile; } public void run() { try { BufferedOutputStream FOS=new BufferedOutputStream(new FileOutputStream(OutFile),BlockSize*3); boolean LastBlockSeen=false; while(!LastBlockSeen) { Block B=MD2Out.consume(); FOS.write(B.Data,0,B.Filled); if (B.LastBlock) LastBlockSeen=true; B.clear(); Out2In.produce(B); } FOS.flush(); FOS.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } } } }