/* Copyright (c) 2007 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.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Collection; import java.util.LinkedList; import java.util.Map; import java.util.Random; import java.util.TreeMap; public class ParityFileTool { /** * @param args */ public static void main(String[] args) { new ParityFileTool(args); } public ParityFileTool(String[] args) { File parityFile=new File(args[0]); LinkedList inputFiles=new LinkedList(); for(int i=1; i sums=new TreeMap(); createParityFile(inputFiles,parityFile,sums,0); System.out.println(sums); } public ParityFileTool() { blockSize=1048576; cacheBlockCount=10; } public int blockSize; public int cacheBlockCount; public int getBlockSize() { return blockSize; } public int getCacheBlockCount() { return cacheBlockCount; } public void setBlockSize(int blockSize) { this.blockSize = blockSize; } public void setCacheBlockCount(int cacheBlockCount) { this.cacheBlockCount = cacheBlockCount; } public void createParityFile(Collection inputFiles, File parityFile) { createParityFile(inputFiles,parityFile,null); } public void createParityFile(Collection inputFiles, File parityFile, Map md5Sums) { createParityFile(inputFiles,parityFile,md5Sums); } public void createParityFile(Collection inputFiles, File parityFile, Map md5Sums, long outputSize) { LinkedList inputFileData=new LinkedList(); boolean md5Flag=false; if (md5Sums!=null) md5Flag=true; { long maxFileSize=0; for(File f : inputFiles) { FileData fd=new FileData(f,md5Flag); inputFileData.add(fd); maxFileSize=Math.max(maxFileSize, fd.getFileSize()); } if (outputSize<=0) outputSize=maxFileSize; } PCMonitor postParity=new PCMonitor(); PCMonitor readyBlock=new PCMonitor(); for(int i=0; i postMD5=new PCMonitor(); paritySumThread=new MDThread(postParity,postMD5); paritySumThread.start(); writerThread=new FileWriterThread(parityFile,postMD5,readyBlock); } else { writerThread=new FileWriterThread(parityFile,postParity,readyBlock); } writerThread.start(); /* * This gets a little messy. This is because the inner loop of the actual * parity calculation has to be run for each byte. Therefore, I'm going out * of my way to make sure the minimum possible amount of logic is in there. * So figuring out how much work can be done in a loop has to be done before. * */ long summedBytes=0; /* while not written complete file... */ while(summedBytes < outputSize) { /* get a list of blocks from files that have blocks left */ LinkedList inputBlocks=new LinkedList(); int blockWriteTarget=0; for(FileData fd : inputFileData) { Block b=fd.getNextBlock(); if (b!=null) { inputBlocks.add(b); blockWriteTarget=Math.max(blockWriteTarget, b.filled); } } /* if this block would go off the deep end */ if (summedBytes + blockWriteTarget > outputSize) { blockWriteTarget=new Long(outputSize-summedBytes).intValue(); } /* get an output block to write in */ Block out=readyBlock.consume(); out.clear(); /* while I haven't filled this block to the size * of the largest input block... */ while(out.filled < blockWriteTarget) { /* * Get the subset of blocks that still have data * that I haven't written yet * * Also, get the size of the smallest one so that we know where the next * stopping point will be */ int nextMin=blockWriteTarget; LinkedList stageBlocks=new LinkedList(); for(Block b : inputBlocks) { if (b.filled > out.filled) { nextMin=Math.min(nextMin, b.filled); stageBlocks.add(b); } } int stageBlockCount=stageBlocks.size(); /* now take this subset of blocks * and put them in a direct array for fast access */ Block blks[]=new Block[stageBlockCount]; int idx=0; for(Block b : stageBlocks) { blks[idx]=b; idx++; } /* the main loop, go from where we stopped last 'out.Filled' * to the established stopping point (smallest block in subset) 'nextMin' */ for(int i=out.filled; i output; private Block currentBlock; private boolean seenLast; private long size; private File f; public FileData(File f,boolean md5Flag) { seenLast=false; readerThread=new FileReaderThread(f); readerThread.start(); size=f.length(); this.f=f; if (md5Flag) { output=new PCMonitor(); sumThread=new MDThread(readerThread.getWrittenBlockMonitor(),output); sumThread.start(); } else { output=readerThread.getWrittenBlockMonitor(); } } public Block getNextBlock() { if (currentBlock!=null) { readerThread.getFreeBlocks().produce(currentBlock); } if (seenLast) return null; currentBlock=output.consume(); if (currentBlock.lastBlock) { seenLast=true; } return currentBlock; } public String getMD5() { return sumThread.getMD5(); } public long getFileSize() { return size; } public File getFile() { return f; } } class FileReaderThread extends Thread { private PCMonitor freeBlocks; private PCMonitor writtenBlocks; private File inputFile; public FileReaderThread(File input) { freeBlocks=new PCMonitor(); writtenBlocks=new PCMonitor(); inputFile=input; for(int i=0; i< cacheBlockCount; i++) { freeBlocks.produce(new Block(blockSize)); } } public void run() { //Random R=new Random(); if (!inputFile.exists()) { System.out.println("Input file: " + inputFile + " does not exist."); System.exit(-1); } try { FileInputStream fis=new FileInputStream(inputFile); long totalSz=inputFile.length(); long doneSz=0; while(doneSz < totalSz) { //try{Thread.sleep(R.nextInt(1000));} //catch(Exception e){} Block blk=freeBlocks.consume(); blk.clear(); while((doneSz < totalSz) && (blk.filled < blockSize)) { int nextRead=new Long(Math.min(totalSz - doneSz, blockSize-blk.filled)).intValue(); int r=fis.read(blk.data,blk.filled,nextRead); if (r>0) { doneSz+=r; blk.filled+=r; } else { System.out.println("End of input file reached before all bytes read!"); System.exit(-1); } } if (doneSz==totalSz) { blk.lastBlock=true; } writtenBlocks.produce(blk); } } 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); } } public PCMonitor getWrittenBlockMonitor() { return writtenBlocks; } public PCMonitor getFreeBlocks() { return freeBlocks; } } class MDThread extends Thread { private String md5Sum; private PCMonitor inputBlocks; private PCMonitor outputBlocks; private MessageDigest sig; private boolean lastBlockSeen; public MDThread(PCMonitor inputBlocks, PCMonitor outputBlocks) { this.inputBlocks=inputBlocks; this.outputBlocks=outputBlocks; } public void run() { try { sig=MessageDigest.getInstance("MD5"); lastBlockSeen=false; while(!lastBlockSeen) { Block B=inputBlocks.consume(); sig.update(B.data,0,B.filled); if (B.lastBlock) lastBlockSeen=true; outputBlocks.produce(B); } } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); System.exit(-1); } } public synchronized String getMD5() { if (md5Sum!=null) return md5Sum; if (!lastBlockSeen) { return "MD5_NOT_READY"; } 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(); return md5Sum; } } class FileWriterThread extends Thread { private File outputFile; private PCMonitor inputBlocks; private PCMonitor returnBlocks; public FileWriterThread(File outputFile, PCMonitor inputBlocks, PCMonitor returnBlocks) { this.outputFile=outputFile; this.inputBlocks=inputBlocks; this.returnBlocks=returnBlocks; } public void run() { try { outputFile.delete(); FileOutputStream fos=new FileOutputStream(outputFile); boolean finished=false; while(!finished) { Block blk=inputBlocks.consume(); fos.write(blk.data, 0, blk.filled); if (blk.lastBlock) { fos.flush(); fos.close(); finished=true; } returnBlocks.produce(blk); } } 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 Block { int filled; byte[] data; boolean lastBlock; public Block(int BlockSize) { filled=0; data=new byte[BlockSize]; lastBlock=false; } public void clear() { filled=0; lastBlock=false; } } }