IO optimization when copying bytes from one file to another

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Skwerl (jusovsky_at_anotherretarded.com)
Date: 10/28/04


Date: Thu, 28 Oct 2004 13:09:03 -0700

Hi guys. I've written code to embed an ICC profile in a TIFF image, and I
think my IO operations are slowing things down. It is taking about a second
to embed each tag in 7-meg TIFF files. Doesn't sound too bad until you try
doing it to 500 files. Basically what I am doing is this:

1. Read header from image, add 12-byte tag to header, and write to a new
TIFF file
2. Update all the offset pointers in other tags in TIFF header to reflect
the change that adding the 12-bytes made and write them to the new TIFF file
3. Copy the rest of the original TIFF to the new one.
4. Append ICC profile (around 100K) to new TIFF file.
5. Delete original TIFF and rename new one to the name of the original

I believe the roblem to be in the method I am using the copy the data from
the original file to the new one. I have pasted it below. Any suggestions
on how to squeeze some more speed out, either in my main algorithm or the
following function? Thanks a bunch!

Josh

private void copyBytes(FileStream source, FileStream destination, long
fromIndex, long length)
   {
       const int chunkSize = 1024;
       long currentIndex = fromIndex;
       long endIndex = fromIndex + length;
       long bytesToCopy = 0;
       byte[] bytes = new byte[chunkSize];
       byte[] endLump;
       byte[] twoBytes = new byte[2];

       source.Seek(fromIndex, SeekOrigin.Begin);
       //Copy a chunk at a time
       for(bytesToCopy = length; bytesToCopy >= chunkSize; bytesToCopy -=
chunkSize)
       {
            source.Read(bytes, 0, chunkSize);
            destination.Write(bytes, 0, chunkSize);
            currentIndex += bytes.Length;
        }

        //Copy the rest now
        endLump = new byte[bytesToCopy];
        source.Read(endLump, 0, endLump.Length);
        destination.Write(endLump, 0, endLump.Length);
        destination.Flush();
}



Relevant Pages

  • Fax Tiff format
    ... I am trying to create a tiff file for submission to MS Fax Server in a format that the server will accept without re-formatting it. ... Speed is very important for our application, so I need to find a way to create a tiff file that MS Fax server will accept without re-formatting it. ...
    (microsoft.public.windowsxp.print_fax)
  • MS Fax Tiff format
    ... I am trying to create a tiff file for submission to MS Fax Server in a format that the server will accept without re-formatting it. ... Speed is very important for our application, so I need to find a way to create a tiff file that MS Fax server will accept without re-formatting it. ...
    (microsoft.public.windows.vista.print_fax_scan)
  • Re: IO optimization when copying bytes from one file to another
    ... I've written code to embed an ICC profile in a TIFF image, ... > the change that adding the 12-bytes made and write them to the new TIFF file ... Copy the rest of the original TIFF to the new one. ... void CopyBytes (Stream source, Stream dest, long fromIndex, ...
    (microsoft.public.dotnet.general)
  • Re: How to create a TIFF image from a binary raw data
    ... You wish to construct a java.awt.Image from a TIFF file, ... TIFF file. ... binary array known consisting of a valid TIFF file. ... I ran your program and it worked well with TIFF image file! ...
    (comp.lang.java.programmer)
  • Re: Problem to read tiff file
    ... Is it possible that your extraction code has a bug. ... Make sure that the tiff file that you extracting from EPS is a valid ... I have extraced a tiff image embedded in an EPS file. ...
    (microsoft.public.dotnet.csharp.general)