2 Replies Latest reply: Feb 24, 2009 12:01 PM by MazAwushu RSS

    Bit by bit copy binary file

    MazAwushu
      Hello everyone,

      I am trying to copy a binary file using a bit by bit method (I'm going to modify the file's code in future, that's why I want to use such method).

      My code is:

      var fmsInstallerFile:File = File.applicationDirectory.resolvePath("bin\\sth.exe");
      var fmsInstallerStream:FileStream = new FileStream();
      fmsInstallerStream.open(fmsInstallerFile, FileMode.READ);

      var file:File = File.desktopDirectory.resolvePath("coppied.exe");
      var stream:FileStream = new FileStream();
      stream.open(file, FileMode.WRITE);

      var tempByte;
      for(var i:int = 0; i < fmsInstallerStream.bytesAvailable-1; i ++)
      {
      fmsInstallerStream.position = i;
      tempByte = fmsInstallerStream.readByte();
      stream.writeByte(tempByte);
      }
      stream.close();


      I don't know why, but this method is not working as it should. I cannot execute the coppied file, and its size is nearly half of the original one. What is wrong in my code?

      Thank you in advance for any reply.
        • 1. Re: Bit by bit copy binary file
          tzeng Adobe Employee
          You probably read fmsInstallerStream.bytesAvailable to a variable.

          • 2. Re: Bit by bit copy binary file
            MazAwushu Community Member
            Finally got it working!
            Thanks tzeng, it was mainly the issue of not reading bytesAvailable property to a variable. However, I will explain this problem to other users if they would encounter the same problem:

            the reason I got EXACLY half of the code coppied is that:
            bytesAbailable property of FileStream object returns the number of bytest from the CURRENT POSITION set by fmsInstallerStream.position to the end of file and NOT always from the begning of a file to its end. That's why when fmsInstallerStream.position was increased on each step of the loop, the loop updated the fmsInstallerStream.bytesAvailable property starting counting from the new (increased by 1) position. So with each step it was retrieving a decreased maximum i value and thus was stopping the loop just in the middle of the file (because i and so the fmsInstallerStream.position were exacly equal or higher than fmsInstallerStream.bytesAvailable property).

            Additionally there was a mistake in the maximum i value anyway. The loop should stop at the value of fmsInstallerStream.bytesAvailable-1, so the if we use a "sharp" (<) comparison, there should be ... ; i < fmsInstallerStream.bytesAvailable; ...

            Hope it shed some light on the issue. To clarify the problem even more, i attach the final code here:

            var fmsInstallerFile:File = File.applicationDirectory.resolvePath("bin\\sth.exe");
            var fmsInstallerStream:FileStream = new FileStream();
            fmsInstallerStream.open(fmsInstallerFile, FileMode.READ);
            fmsInstallerStream.position = 0;

            var file:File = File.desktopDirectory.resolvePath("coppied.exe");
            var stream:FileStream = new FileStream();
            stream.open(file, FileMode.WRITE);

            var tempByte;
            var totalBytes = fmsInstallerStream.bytesAvailable;
            for(var i:int = 0; i < totalBytes; i ++)
            {
            fmsInstallerStream.position = i;
            tempByte = fmsInstallerStream.readByte();
            stream.writeByte(tempByte);
            }
            fmsInstallerStream.close();
            stream.close();


            Bests,
            Maciek,
            Poland.