Example #1
0
        internal static void Serialize(TagModel tagModel, Stream stream)
        {
            // Skip the header 10 bytes for now, we will come back and write the Header
            // with the correct size once have the tag size + padding
            stream.Seek(10, SeekOrigin.Begin);

            // Write the frames in binary format
            using (var writer = new TagWriter(stream))
                foreach (var frame in tagModel.Frames)
                {
#if NETSTANDARD2_0
                    writer.Write(frame.FrameId.ToCharArray());
#else
                    writer.Write(frame.FrameId.AsSpan());
#endif

                    // Skip the size bytes for now
                    var sizeIndex = stream.Position;
                    stream.Seek(4, SeekOrigin.Current);

                    // Set the FileAlter flag, if requested
                    writer.Write((short)(frame.FileAlter ? tagModel.Header.Version == 4 ? 0b0010_0000 : 0b0100_0000 : 0));

                    frame.Write(stream);
                    var frameSize = (uint)(stream.Position - sizeIndex - 6);

                    // Now update the size
                    stream.Seek(sizeIndex, SeekOrigin.Begin);
                    if (tagModel.Header.Version == 4)
                    {
                        writer.WriteSyncSafe(frameSize);
                    }
                    else
                    {
                        writer.WriteBigEndian(frameSize);
                    }
                    stream.Seek(2 + frameSize, SeekOrigin.Current);
                }

            // update the TagSize stored in the tagModel
            var id3TagSize = (uint)stream.Position - 10;

            tagModel.Header.TagSize = id3TagSize;

            // Write the padding
            for (var i = 0; i < tagModel.Header.PaddingSize; i++)
            {
                stream.WriteByte(0);
            }

            // Now seek back to the start and write the header
            var position = stream.Position;

            stream.Seek(0, SeekOrigin.Begin);
            tagModel.Header.Serialize(stream);

            stream.Position = position;
        }
Example #2
0
 internal void Serialize(Stream stream)
 {
     using (var writer = new TagWriter(stream))
     {
         writer.Write(_id3);
         writer.Write(Version);
         writer.Write((byte)0);
         writer.Write((byte)0);
         writer.WriteSyncSafe(TagSize + PaddingSize);
     }
 }