public void WriteInt32Test()
 {
     var fio = new Mock<IFileIO>(MockBehavior.Strict);
     fio.Setup(io => io.WriteInt32(1743));
     FormattedWriter fw = new FormattedWriter(fio.Object);
     fw.WriteInt32(1743);
     fio.Verify(f => f.WriteInt32(1743), Times.Once());
 }
        public void WriteInt32Test()
        {
            var fio = new Mock <IFileIO>(MockBehavior.Strict);

            fio.Setup(io => io.WriteInt32(1743));
            FormattedWriter fw = new FormattedWriter(fio.Object);

            fw.WriteInt32(1743);
            fio.Verify(f => f.WriteInt32(1743), Times.Once());
        }
        /// <summary>
        /// Creates the header's sections from the descriptions found in the context. Computes their positions 
        /// and sets the FirstItemPosition property in the context.
        /// </summary>
        internal byte[] CreateSections(WriteContext wc)
        {
            var saved = wc.Writer;

            using (var sectionStream = new MemoryStream()) // see noop-Dispose comment below
            {
                var sectionWriter = new FormattedWriter(new FileIO(sectionStream));
                int pos = 32; // sections start at byte position 32
                foreach (var formatter in this.sectionFormatters)
                {
                    // payload
                    using (var payloadStream = new MemoryStream()) // actually MemoryStream.Dispose() is a noop here, but for the code analysers pleasure we wrap these usings around
                    {
                        wc.Writer = new FormattedWriter(new FileIO(payloadStream));
                        formatter.Write(wc);
                        var size = (int)payloadStream.Length;
                        if (size > 0)
                        {
                            // section id
                            sectionWriter.WriteInt32(formatter.Id);
                            pos += 4;

                            // nextSectionOffset
                            sectionWriter.WriteInt32(size);
                            pos += 4;

                            // payload
                            sectionWriter.WriteRaw(payloadStream.ToArray());
                            pos += size; // no padding or spacing done here

                            wc.SectionCount++;
                        }
                    }
                }

                // padding
                int paddingBytes = (8 - pos % 8);
                if (paddingBytes == 8) paddingBytes = 0;
                sectionWriter.WriteRaw(new byte[paddingBytes]);
                wc.ItemAreaStart = pos + paddingBytes; // first item starts padded on 8 byte boundary.

                wc.Writer = saved;
                return sectionStream.ToArray();
            }
        }