8 Replies Latest reply on Aug 28, 2007 4:41 AM by EJP

    Copy files using checked input and output streams

    807605
      Is the Following code correct for copying a file



                     if (!destination.exists()) {
                          destination.createNewFile();
                     }
                     CheckedInputStream cin = null;
                     CheckedOutputStream cout = null;
                     
                     boolean isCSSame = false;
                     try {
                          cin = new CheckedInputStream(new FileInputStream(source),new CRC32());
                          cout = new CheckedOutputStream(new FileOutputStream(destination), new CRC32());
                          byte[] buf = new byte[1024];
                          int len;
                          while ((len = cin.read(buf)) > 0) {
                               cout.write(buf, 0, len);
                          }
                          if(cin.getChecksum().getValue() == cout.getChecksum().getValue()){
                               isCSSame = true;
                          }
                     } finally {
                          cin.close();
                          cout.close();
                     }
                     
                     if(!isCSSame){
                          System.out.println("Files Could not be copied correctly Checksum failed");
                     }
        • 1. Re: Copy files using checked input and output streams
          807605
          What makes you think it is not OK?
          • 2. Re: Copy files using checked input and output streams
            796365
            You tell us - did it give correct results when you tested it?

            If not, what was wrong - details, please.
            • 3. Re: Copy files using checked input and output streams
              807605
              Hi This code will work

              import java.io.File;
              import java.io.FileInputStream;
              import java.io.FileOutputStream;
              import java.io.IOException;
              import java.util.zip.CRC32;
              import java.util.zip.CheckedInputStream;
              import java.util.zip.CheckedOutputStream;

              class Copying
              {
                   public static void main(String[] args) throws IOException {
                        
                        File destination = new File("c:/Copying/destination.txt");
                        File source = new File("c:/Copying/source.txt");
                        if (!destination.exists()) {
                             destination.createNewFile();
                        }
                        CheckedInputStream cin = null;
                        CheckedOutputStream cout = null;
                        
                        boolean isCSSame = false;
                        try {
                             cin = new CheckedInputStream(new FileInputStream(source),new CRC32());
                             cout = new CheckedOutputStream(new FileOutputStream(destination), new CRC32());
                             byte[] buf = new byte[1024];
                             int len;
                             while ((len = cin.read(buf)) > 0) {
                                  cout.write(buf, 0, len);
                             }
                             if(cin.getChecksum().getValue() == cout.getChecksum().getValue()){
                                  isCSSame = true;
                             }
                        } finally {
                             cin.close();
                             cout.close();
                        }
                        
                        if(!isCSSame){
                             System.out.println("Files Could not be copied correctly Checksum failed");
                        }
                   }
              }
              • 4. Re: Copy files using checked input and output streams
                807605
                Hi,

                This code which i posted is working but i was not sure whether checked input stream and output stream could be used for actual copying of files.
                • 5. Re: Copy files using checked input and output streams
                  807605
                  Hi,

                  This code which i posted is working but i was not
                  sure whether checked input stream and output stream
                  could be used for actual copying of files.
                  The implication is that you copied this code from somewhere. May we know where?
                  • 6. Re: Copy files using checked input and output streams
                    EJP
                    BTW the correct way to check a checksum is to append it to the stream at the writing end. At the reading end, the checksum of the total stream including the checksum should be zero (regardless of which polynomial, initial value, etc you are using).

                    That way you are including the CRC bytes in the CRC calculation at the receiving end, giving you much more certainty over the integrity of the data stream.

                    Make sure not to write the checksum bytes to the target file at the receiving end.

                    On the other hand if you are using TCP/IP it already does checksumming at two levels, TCP and IP, and this third checksum at the application level is just a waste of time.
                    • 7. Re: Copy files using checked input and output streams
                      807605
                      No i was using fileinputstream
                      so tried replacing it with checked input stream
                      ie all

                      thanks i got the answer
                      • 8. Re: Copy files using checked input and output streams
                        EJP
                        Still a complete waste of time. The only time you need a CRC is over an unreliable transport, e.g. UDP or a modem being used directly (i.e. not via TCP/PPP). FileInputStream and FileOutputStream are not unreliable transports in that sense.