I have the "comparison before saving" stuff working and that is cutting down my upload times dramatically. This is almost like a real blogging tool now. However, one function -- called TransformAndSaveDoc -- turned out really nasty. This function transforms an xml document with an xslt style sheet. It then compares the result to what is already on disk and only writes the new result to disk if the new version is different from the old version.
Suggestions for making this tighter? Is there some functionality (memcmp?) that I'm missing in the .Net framework? Since I don't have comments working, send mail to eightypercent@bedafamily.com and I'll post the best comments up here with credit and eternal thanks.
private void TransformAndSaveDoc(XslTransform xslt, XmlDocument xmlSource, string fileName) { using (MemoryStream memstrm = new MemoryStream()) { bool fFileDifferent = false; xslt.Transform(xmlSource, null, memstrm); FileInfo fi = new FileInfo(fileName); // Check for simple things first if (!File.Exists(fileName) || fi.Length != memstrm.Length) fFileDifferent = true; // Actually compare the file to the stream if (!fFileDifferent) { Byte[] buffer1 = new Byte[8096]; Byte[] buffer2 = new Byte[8096]; // compare the existing file with the memory stream using (FileStream fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { memstrm.Seek(0, SeekOrigin.Begin); int size1 = 0, size2 = 0; while( !fFileDifferent && (0 != (size1 = memstrm.Read(buffer1, 0, 8096)))) { size2 = fileStream.Read(buffer2, 0, 8096); Debug.Assert(size1 == size2); for (int i = 0; i < size1; i++) { if (buffer1[i] != buffer2[i]) { fFileDifferent = true; break; } } } } } // Write that puppy out if (fFileDifferent) { using (FileStream fileStream = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) { memstrm.Seek(0, SeekOrigin.Begin); Byte[] buffer = new Byte[8096]; int size; while(0 != (size = memstrm.Read(buffer, 0, 8096))) { fileStream.Write(buffer, 0, size); } } } } }