private ulong WriteContent(Stream output)
{
// Before doing anything else, see if we have multiple FileAttributes and fix this
if (SwfFile.Configuration.FixMultipleFileAttributes)
{
this.FixFileAttributes();
Log.Debug(this, "Fixing File Attributes");
}
Log.Debug(this, "Writing stream for " + TagHandlers.Count.ToString("d") + " tags");
// This call collects the required version information.
// Since version also decides size, this has to be done first.
if (this.WriteVersion > this.Version)
{
this.Version = this.WriteVersion;
Log.Debug(this, "Writing in version " + this.Version.ToString("d"));
}
ulong lengthOverAll = 0;
Log.Debug(this, "Calculating Length");
for (int i = 0; i < TagHandlers.Count; i++)
{
// Now calculate length
try
{
lengthOverAll += (ulong)TagHandlers[i].TagAndLengthEncoded.Length;
lengthOverAll += TagHandlers[i].Length;
}
catch (Exception e)
{
throw e;
}
}
for (int i = 0; i < TagHandlers.Count; i++)
{
long posBefore = output.Position;
ulong tagHandlerLength = this.TagHandlers[i].Length;
Log.Debug(this, "Writing Tag #" + i + "(" + TagHandlers[i].Tag.TagTypeName + ")" + " Total-Length : 0x" + TagHandlers[i].Tag.LengthTotal.ToString("X08") + " @ Stream-Poistion : 0x" + output.Position.ToString("X08") + 1);
TagHandlers[i].Write(output);
// Fire WriteProgressChanged event
if (null != WriteProgressChanged)
{
WriteProgressChanged(this, new SwfWriteProgressChangedEventArgs(TagHandlers[i].Tag.TagType, output.Length, output.Position));
}
if ((ulong)(output.Position - posBefore) != (tagHandlerLength + (ulong)TagHandlers[i].TagAndLengthEncoded.Length))
{
Exception e = new Exception("Critical internal error: "
+ TagHandlers[i].GetType().ToString() + " wrote "
+ (ulong)(output.Position - posBefore) + " bytes, but declared "
+ (ulong)(tagHandlerLength + (ulong)TagHandlers[i].TagAndLengthEncoded.Length) + " bytes length"
);
Log.Error(this, e);
throw e;
}
}
if (lengthOverAll != (ulong)output.Length)
{
Exception e = new Exception("Critical internal error: calculated length " + lengthOverAll.ToString("d") + " != output stream length " + output.Length);
Log.Error(this, e);
throw e;
}
return lengthOverAll;
}