/* * @(#)BadFileCopier.java (MetaWerx servlet examples) * * Copyright (c) 2003 metawerx. All Rights Reserved. * * This software is the confidential and proprietary information of Metawerx * ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the * license agreement you entered into with Metawerx. * * METAWERX MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE OR THE SOURCE CODE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR * A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. METAWERX SHALL NOT BE LIABLE * FOR ANY DAMAGES SUFFERED BY ANY PARTY AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE AND DOCUMENTATION OR ITS DERIVATIVES * * Usage: * java BadFileCopier * */ import java.io.*; public class BadFileCopier { public static void main(String s[]) throws FileNotFoundException, IOException { System.out.println("Source file: "+s[0]); if(s.length != 2) { System.out.println("java BadFileCopier "); } else { if(s[1].endsWith("/") || s[1].endsWith("\\")) s[1] = s[1].substring(0, s[1].length()-1); String filename = new File(s[0]).getName(); String destFileName = s[1]; if(new File(s[1]).isDirectory()) { System.out.println("Dest file is directory"); System.out.println("Using source filename: "+filename); destFileName = destFileName + "\\" + filename; System.out.println("New dest file: "+destFileName); } else System.out.println("Dest file: "+destFileName); RandomAccessFile source = new RandomAccessFile(s[0], "r"); RandomAccessFile dest = new RandomAccessFile(destFileName, "rw"); File sfile = new File(s[0]); long len = sfile.length(); int buflen = 65536; byte[] buffer = new byte[buflen]; int readLen = 0; int offset = 0; int errors = 0; while(offset < len) { boolean ok = false; try { System.out.print(offset+"-"+(offset+buflen)); source.seek(offset); readLen = source.read(buffer, 0, buflen); if(readLen != -1) { dest.seek(offset); dest.write(buffer, 0, readLen); offset = offset + readLen; ok = true; System.out.println(" OK "+readLen); } } catch(Exception e) { System.out.println(" - ERROR "+e.getMessage()+" - SKIPPED - WRITING 0's"); errors++; } if(ok == false) { for(int i = 0; i < buflen; i++) buffer[i] = 0; dest.seek(offset); dest.write(buffer, 0, buflen); offset = offset + buflen; } } System.out.println("COPY COMPLETED, TOTAL BAD BLOCKS SKIPPED: "+errors); } } }