import java.io.*;

/* The class is used for copying from a DataInputStream to a DataOutputStream
 */


public class CopyFile {

   private static final int BUFSIZE = 1024;
   private static boolean verbose = false;
   private DataInputStream fromStream;
   private DataOutputStream toStream;

   public CopyFile(DataInputStream from, DataOutputStream to) {
      fromStream = from;
      toStream = to;
   }

   public void setVerbose(boolean f) {
      verbose = f;
   }

   /* Copy until an error occurs.
      Return the number of bytes successfully copied.
      The most common error is an end of file on input
    */
   public int copyAll() {
      int numCopied = 0;
      int numRead = 0;
      byte[] buffer = new byte[BUFSIZE];
      try {
         do {
            numRead = fromStream.read(buffer);
            if (verbose)
               System.out.println("bytes read: " + numRead);
            if (numRead > 0) {
               toStream.write(buffer, 0, numRead);
               if (verbose)
                  System.out.println("bytes written: " + numRead);
               numCopied = numCopied + numRead;
            }
         } while (numRead != -1);
      } catch (IOException e) {
         if (verbose)
            System.out.println("IO error in copyAll");
      }
      return numCopied;
   }
}
