home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / filecpy2.zip / filecopy.java < prev    next >
Text File  |  1999-12-04  |  6KB  |  204 lines

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.zip.*;
  4. import java.applet.*;
  5.  
  6. public class filecopy {
  7.  
  8.  public static void main(String[] args) {
  9. // Test if parameters have been specified
  10.    filecopy f = new filecopy();
  11.    String infilename=new String("InputFile");
  12.    int argoff=0;
  13.    for (int i=0;i<args.length;i++) {
  14.      if (args[i].equalsIgnoreCase("-help")||args[i].equalsIgnoreCase("-h")||args[i].equalsIgnoreCase("-?")) {
  15.      System.out.println("Usage: filecopy [-skip <bytes>] [-byte <value>] [-q] [-qq] InputFile OutputFile");
  16.      System.out.println("");
  17.      System.out.println("-skip <bytes> Defines how many bytes should be skipped if a read error occurs.");
  18.      System.out.println("-byte <value> Defines the value to be inserted where the read error occurs.");
  19.      System.out.println("-q            Quiet. Does not show pregress indicator.");
  20.      System.out.println("-qq           Extra Quiet. Does not show pregress indicator or errors.");
  21.      System.out.println("InputFile     The filename of the file to be read.");
  22.      System.out.println("OutputFile    The filename to write the result to.");
  23.      System.out.println("");
  24.      System.out.println("Filecopy was written in 1999 by Klaus Post, aka sh0dan of VoxPod.");
  25.      System.out.println("email: sh0dan@voxpod.dk");
  26.      System.out.println("");
  27.      System.exit(0);
  28.      }
  29.      System.out.println("");
  30.      if (args[i].equalsIgnoreCase("-skip")) {
  31.        if (i==(args.length-1)) {
  32.          System.out.println("Error: -skip must be followed by a value");
  33.          System.out.println("Setting skip to 128 bytes");
  34.          argoff++;
  35.        } else {
  36.          try {
  37.            defskip=Integer.parseInt(args[argoff+1]);
  38.          } catch (NumberFormatException n) {
  39.            System.out.println("Error: -skip must be followed by a value");
  40.            System.out.println("Setting skip to 128 bytes");
  41.            defskip=128;
  42.            argoff--;
  43.          }
  44.          if (defskip<=0) {
  45.             System.out.println("Warning: -skip must have a value bigger than 0.");
  46.             System.out.println("Setting skip to 128 bytes");
  47.             defskip=128;
  48.          }
  49.          argoff+=2;
  50.        }
  51.      }
  52.      if (args[i].equalsIgnoreCase("-byte")) {
  53.        if (i==(args.length-1)) {
  54.          System.out.println("Error: -byte must be followed by a value");
  55.          argoff++;
  56.        } else {
  57.          try {
  58.            defbyte=Integer.parseInt(args[argoff+1]);
  59.          } catch (NumberFormatException n) {
  60.             System.out.println("Error: -byte must be followed by a value");
  61.             argoff--;
  62.             defbyte=0;
  63.          }
  64.          if (defbyte<0 || defbyte>255) {
  65.             System.out.println("Error: -byte must have a value bigger than 0 and less than 255.");
  66.             System.exit(1);
  67.          }
  68.          argoff+=2;
  69.        }
  70.      }
  71.      if (args[i].equalsIgnoreCase("-q")) {
  72.        quiet=1;
  73.        argoff++;
  74.      }
  75.      if (args[i].equalsIgnoreCase("-qq")) {
  76.        quiet=2;
  77.        argoff++;
  78.      }
  79.    }
  80.    if (args.length>argoff) {
  81.      infilename=new String(args[argoff]);
  82.      argoff++;
  83.    } else {
  84.      System.out.println("You must specify an InputFile");
  85.      System.exit(1);
  86.    }
  87.    String outfilename=new String(infilename+"_out");
  88.    if (args.length>argoff) {
  89.      outfilename=new String(args[argoff]);
  90.      argoff++;
  91.    }
  92.    f.doit(infilename,outfilename);
  93.  }
  94.    static int quiet=0;
  95.    File ifile;
  96.    File ofile;
  97.    File dofile;
  98.    File lfile;
  99.    FileInputStream fis;
  100.    FileInputStream lfis;
  101.    FileInputStream raf;
  102.    BufferedInputStream bis;
  103.    BufferedInputStream lbis;
  104.    FileOutputStream fos;
  105.    FileOutputStream dfos;
  106.    BufferedOutputStream bos;
  107.    BufferedOutputStream dbos;
  108.    StringBuffer name;
  109.    StringBuffer chars;
  110.    int files;
  111.    int out;
  112.    static int defbyte=0;
  113.    static int defskip=128;
  114.  
  115.  public void doit(String infile, String outfile) {
  116.    try {
  117.    System.out.println("Engaging sh0/filecopy v0.02y+  by sh0dan/VoxPod 1999.");
  118.    System.out.println("");
  119. // Everything should be OK now! Create Inputstreams
  120.  
  121.      lfile = new File(infile);
  122.  
  123.      File datafile = new File(outfile);
  124.  
  125.      dfos = new FileOutputStream(datafile);
  126.      dbos = new BufferedOutputStream(dfos);
  127.      raf = new FileInputStream(lfile);
  128.      bis = new BufferedInputStream(raf);
  129.      data=bis.read();
  130.      out=1;
  131.      if (quiet <2) {
  132.        System.out.println("Copying data.");
  133.      }
  134.      do {
  135.        dbos.write(data);
  136.        out++;
  137.        if (out%1024==0)  {
  138.          if (quiet <1) System.out.print("#");
  139.          if (out%(32*1024)==0)  {
  140.            System.gc();
  141.          }
  142.        }
  143.        try {
  144.          data=bis.read();
  145.        } catch (IOException ex) {
  146.          data=defbyte;
  147.          if (quiet <2)
  148.            System.out.println("Read failed at byte "+out+"; Reopening file with offset "+defskip+"!");
  149.          skipread(lfile,dbos,out);
  150.          out+=defskip;
  151.        }
  152.      } while (data!=-1);
  153.  
  154.      dbos.flush();
  155.      dfos.flush();
  156.      dbos.close();
  157.      dfos.close();
  158.  
  159.    } catch (IOException ex) {
  160.      System.out.println("Unexpected IO-exception in main:"+ex);
  161.      ex.printStackTrace();
  162.      System.exit(2);
  163.    }
  164.  
  165.  }
  166.  
  167.    static int data;
  168.  
  169.  private void skipread(File infile, OutputStream out, int offset) throws IOException {
  170.    raf = new FileInputStream(lfile);
  171.    raf.skip((long)(offset+defskip));
  172.    bis = new BufferedInputStream(raf);
  173.    try {
  174.      data=bis.read();
  175.      if (quiet <2)
  176.       System.out.println("Read succesful after skip - reading bytes indiviudally!");
  177.    } catch (IOException ex) {
  178.      if (quiet <2)
  179.        System.out.println("Read failed after skip - filling with default bytes!");
  180.       for (int i=0;i<defskip;i++) {
  181.          dbos.write(defbyte);
  182.       }
  183.       return;
  184.    }
  185.    for (int i=0;i<defskip;i++) {
  186.       raf = new FileInputStream(lfile);
  187.       raf.skip((long)(offset+i));
  188.       bis = new BufferedInputStream(raf);
  189.       try {
  190.         data=bis.read();
  191.         if (data==-1) return;
  192.         if (quiet <1)
  193.           System.out.print("+");
  194.         dbos.write(data);
  195.       } catch (IOException ex) {
  196.        if (quiet <1)
  197.            System.out.print("-");
  198.          dbos.write(defbyte);
  199.       }
  200.    }
  201.  }
  202. }
  203.  
  204.