/// <summary> /// Writes the (compressed or uncompressed) swf data to a stream. /// The stream gets flushed and closed afterwards. /// </summary> /// <param name="swf">Swf</param> public void Write(Swf swf) { if (swf == null) { return; } // add EndTag is is not the last one BaseTag lastTag = swf.Tags.GetLastOne(); if (lastTag == null || !(lastTag is EndTag)) { swf.Tags.Add(new EndTag()); } // update tag lengths to adapt to bytecode length swf.UpdateData(); SwfHeader header = swf.Header; // ASCII seems to be ok for Flash 5 and 6+ as well BufferedBinaryWriter writer = new BufferedBinaryWriter(baseStream, System.Text.Encoding.GetEncoding("ascii")); BufferedBinaryWriter dataWriter = writer; bool isCompressed = (header.Signature[0] == 'C'); if (isCompressed && swf.Version >= 6) { // SharpZipLib makes it easy for us, simply // chain a Deflater into the stream DeflaterOutputStream def = new DeflaterOutputStream(baseStream); dataWriter = new BufferedBinaryWriter(def); } // writer header data, always uncompressed writer.WriteString(header.Signature, 3); writer.Write(swf.Version); writer.Write(swf.ByteCount); writer.Flush(); // write header data pt.2, using either // original stream or deflater stream header.Size.WriteTo(dataWriter); dataWriter.SynchBits(); dataWriter.WriteFWord(header.Fps, 8, 8); dataWriter.Write(header.Frames); // write tags data IEnumerator tags = swf.Tags.GetEnumerator(); while (tags.MoveNext()) { BaseTag tagToWrite = (BaseTag)tags.Current; dataWriter.Write(tagToWrite.Data); } // flush + close dataWriter.Flush(); dataWriter.Close(); }
/// <summary> /// Writes the (compressed or uncompressed) swf data to a stream. /// The stream gets flushed and closed afterwards. /// </summary> /// <param name="swf">Swf</param> public void Write(Swf swf) { if (swf == null) return; // add EndTag is is not the last one BaseTag lastTag = swf.Tags.GetLastOne(); if (lastTag == null || !(lastTag is EndTag)) swf.Tags.Add(new EndTag()); // update tag lengths to adapt to bytecode length swf.UpdateData(); SwfHeader header = swf.Header; // ASCII seems to be ok for Flash 5 and 6+ as well BufferedBinaryWriter writer = new BufferedBinaryWriter(baseStream, System.Text.Encoding.GetEncoding("ascii")); BufferedBinaryWriter dataWriter = writer; bool isCompressed = (header.Signature[0] == 'C'); if (isCompressed && swf.Version >= 6) { // SharpZipLib makes it easy for us, simply // chain a Deflater into the stream DeflaterOutputStream def = new DeflaterOutputStream(baseStream); dataWriter = new BufferedBinaryWriter(def); } // writer header data, always uncompressed writer.WriteString(header.Signature, 3); writer.Write(swf.Version); writer.Write(swf.ByteCount); writer.Flush(); // write header data pt.2, using either // original stream or deflater stream header.Size.WriteTo(dataWriter); dataWriter.SynchBits(); dataWriter.WriteFWord(header.Fps, 8, 8); dataWriter.Write(header.Frames); // write tags data IEnumerator tags = swf.Tags.GetEnumerator(); while (tags.MoveNext()) { BaseTag tagToWrite = (BaseTag)tags.Current; dataWriter.Write(tagToWrite.Data); } // flush + close dataWriter.Flush(); dataWriter.Close(); }