/// <summary> /// Takes a SWF object and turns it into a .swf binary file. It might seem nicer to /// write it to a stream, but the stream has a file length at the start which we only /// know once we've written it all out to a byte array anyway. Returning it as a /// chunk of data is simply more honest; an output stream would promise streaminess behind /// the scenes that we cannot deliver. /// </summary> /// <returns>The .swf data is returned as a byte array.</returns> public byte[] ToByteArray() { /* We start writing things mid-way through the header, at the point * where compression comes into effect. Once we've created and perhaps * compressed the data, we can write the first part of the header and * concatenate the rest. */ writtenJPEGTable = null; /* The file contains tags, which can contain tags, each of which is prefixed with a length. * To track all this, we use a stack of writers, each with its own buffer. The first on the * stack is the buffer that will contain the file itself. */ this.writers = new Stack <WriteBuffer>(); this.swfOut = new WriteBuffer(Tag.None, "swf"); this.writers.Push(this.swfOut); this.swfOut.WriteRect(new Rect(0, this.swf.FrameWidth, 0, this.swf.FrameHeight)); this.swfOut.WriteFIXED8(this.swf.Fps); this.swfOut.WriteUI16(this.swf.FrameCount); this.WriteTags(); /* Ok, we basically have a SWF now. All we need to do is compress it and * stick a header on at the front... */ byte[] body = this.swfOut.Data; uint fileLen = (uint)(body.Length + 8); /* Add the 8 bytes of header we haven't done yet. */ if (this.options.Compressed) { MemoryStream zbytes = new MemoryStream(); DeflaterOutputStream zos = new DeflaterOutputStream(zbytes); zos.Write(body, 0, body.Length); zos.Close(); body = zbytes.ToArray(); } MemoryStream final = new MemoryStream(); SWFDataTypeWriter finalWriter = new SWFDataTypeWriter(final); finalWriter.WriteUI24(this.options.Compressed ? SIG_COMPRESSED : SIG_UNCOMPRESSED); /* ISSUE 27: Hard-coded SWF version 10. Technically this should be an option but * for now we don't want the headache of version-specific code. */ finalWriter.WriteUI8(SWF_VERSION); finalWriter.WriteUI32(fileLen); finalWriter.Write(body, 0, body.Length); return(final.ToArray()); }
/// <summary> /// Takes a SWF object and turns it into a .swf binary file. It might seem nicer to /// write it to a stream, but the stream has a file length at the start which we only /// know once we've written it all out to a byte array anyway. Returning it as a /// chunk of data is simply more honest; an output stream would promise streaminess behind /// the scenes that we cannot deliver. /// </summary> /// <returns>The .swf data is returned as a byte array.</returns> public byte[] ToByteArray() { /* We start writing things mid-way through the header, at the point * where compression comes into effect. Once we've created and perhaps * compressed the data, we can write the first part of the header and * concatenate the rest. */ writtenJPEGTable = null; /* The file contains tags, which can contain tags, each of which is prefixed with a length. * To track all this, we use a stack of writers, each with its own buffer. The first on the * stack is the buffer that will contain the file itself. */ this.writers = new Stack<WriteBuffer>(); this.swfOut = new WriteBuffer(Tag.None, "swf"); this.writers.Push(this.swfOut); this.swfOut.WriteRect(new Rect(0, this.swf.FrameWidth, 0, this.swf.FrameHeight)); this.swfOut.WriteFIXED8(this.swf.Fps); this.swfOut.WriteUI16(this.swf.FrameCount); this.WriteTags(); /* Ok, we basically have a SWF now. All we need to do is compress it and * stick a header on at the front... */ byte[] body = this.swfOut.Data; uint fileLen = (uint)(body.Length + 8); /* Add the 8 bytes of header we haven't done yet. */ if (this.options.Compressed) { MemoryStream zbytes = new MemoryStream(); DeflaterOutputStream zos = new DeflaterOutputStream(zbytes); zos.Write(body, 0, body.Length); zos.Close(); body = zbytes.ToArray(); } MemoryStream final = new MemoryStream(); SWFDataTypeWriter finalWriter = new SWFDataTypeWriter(final); finalWriter.WriteUI24(this.options.Compressed ? SIG_COMPRESSED : SIG_UNCOMPRESSED); /* ISSUE 27: Hard-coded SWF version 10. Technically this should be an option but * for now we don't want the headache of version-specific code. */ finalWriter.WriteUI8(SWF_VERSION); finalWriter.WriteUI32(fileLen); finalWriter.Write(body, 0, body.Length); return final.ToArray(); }