public void Write(WriteContext c)
        {
            if (c.Description == null)
            {
                return;
            }
            if (c.Description.ItemDescription == null)
            {
                return;
            }
            var timeFields = c.Description.ItemDescription.Fields.Where(f => f.IsTime).ToList();

            if (!timeFields.Any())
            {
                return;
            }

            // time format
            if (!c.Description.Timescale.HasValue)
            {
                // TeaFile<T>.Create will assign the default Timescale to the descrption,
                // so Timescale should always be set.
                throw new InternalErrorException();
            }
            c.Writer.WriteInt64(c.Description.Timescale.Value.Epoch);
            c.Writer.WriteInt64(c.Description.Timescale.Value.TicksPerDay);

            // time fields
            c.Writer.WriteInt32(timeFields.Count);
            timeFields.ForEach(f => c.Writer.WriteInt32(f.Offset));
        }
 public void Write(WriteContext c)
 {
     if (c == null) throw new ArgumentNullException("c");
     if (c.Description == null) return;
     if (!c.Description.ContentDescription.IsSet()) return;
     c.Writer.WriteText(c.Description.ContentDescription);
 }
 public void CreateSectionsTest()
 {
     var fw = new Mock<IFormattedWriter>(MockBehavior.Strict);
     var wc = new WriteContext(fw.Object);
     wc.ItemAreaStart.Should().Be(0); //  not set yet
     HeaderManager.Instance.CreateSections(wc);
     wc.ItemAreaStart.Should().Be(32);
 }
        public void CreateSectionsTest()
        {
            var fw = new Mock <IFormattedWriter>(MockBehavior.Strict);
            var wc = new WriteContext(fw.Object);

            wc.ItemAreaStart.Should().Be(0); //  not set yet
            HeaderManager.Instance.CreateSections(wc);
            wc.ItemAreaStart.Should().Be(32);
        }
        public void Write(WriteContext c)
        {
            if (c == null) throw new ArgumentNullException("c");
            if (c.Description == null) return;
            if (c.Description.NameValues == null) return;
            if (c.Description.NameValues.Count == 0) return;

            var nvs = c.Description.NameValues;
            c.Writer.WriteInt32(nvs.Count());
            nvs.ForEach(nv => c.Writer.WriteNameValue(nv));
        }
        /// <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());
            }
        }
Beispiel #7
0
 public void Write(WriteContext c)
 {
     if (c == null)
     {
         throw new ArgumentNullException("c");
     }
     if (c.Description == null)
     {
         return;
     }
     if (!c.Description.ContentDescription.IsSet())
     {
         return;
     }
     c.Writer.WriteText(c.Description.ContentDescription);
 }
        public void Write(WriteContext c)
        {
            if (c.Description == null) return;
            if (c.Description.ItemDescription == null) return;
            var timeFields = c.Description.ItemDescription.Fields.Where(f => f.IsTime).ToList();
            if (!timeFields.Any()) return;

            // time format
            if (!c.Description.Timescale.HasValue)
            {
                // TeaFile<T>.Create will assign the default Timescale to the descrption,
                // so Timescale should always be set.
                throw new InternalErrorException();
            }
            c.Writer.WriteInt64(c.Description.Timescale.Value.Epoch);
            c.Writer.WriteInt64(c.Description.Timescale.Value.TicksPerDay);

            // time fields
            c.Writer.WriteInt32(timeFields.Count);
            timeFields.ForEach(f => c.Writer.WriteInt32(f.Offset));
        }
        public WriteContext WriteHeader(IFormattedWriter w, TeaFileDescription description)
        {
            var wc = new WriteContext(w);

            wc.ItemAreaStart = 32; //  if no sections are created, ItemArea will start at 32. This value will change is sections are created

            wc.Description = description;
            byte[] sections = this.CreateSections(wc);

            w.WriteInt64(0x0d0e0a0402080500);
            w.WriteInt64(wc.ItemAreaStart);
            w.WriteInt64(0);
            w.WriteInt64(wc.SectionCount);
            wc.Writer.WriteRaw(sections);

            //var sectionPadding = wc.ItemAreaStart - w.Position;
            //if (sectionPadding < 0) throw new FileFormatException("Stream position (potentially after writing sections) is behind item start. After opening a TeaFile and reading the header, the file pointer must be exactly at the begin of the item area.");
            //w.WriteZeroBytes((int) sectionPadding);

            return(wc);
        }
        public void Write(WriteContext c)
        {
            if (c == null)
            {
                throw new ArgumentNullException("c");
            }
            if (c.Description == null)
            {
                return;
            }
            if (c.Description.NameValues == null)
            {
                return;
            }
            if (c.Description.NameValues.Count == 0)
            {
                return;
            }

            var nvs = c.Description.NameValues;

            c.Writer.WriteInt32(nvs.Count());
            nvs.ForEach(nv => c.Writer.WriteNameValue(nv));
        }
        public WriteContext WriteHeader(IFormattedWriter w, TeaFileDescription description)
        {
            var wc = new WriteContext(w);
            wc.ItemAreaStart = 32; //  if no sections are created, ItemArea will start at 32. This value will change is sections are created

            wc.Description = description;
            byte[] sections = this.CreateSections(wc);

            w.WriteInt64(0x0d0e0a0402080500);
            w.WriteInt64(wc.ItemAreaStart);
            w.WriteInt64(0);
            w.WriteInt64(wc.SectionCount);
            wc.Writer.WriteRaw(sections);

            //var sectionPadding = wc.ItemAreaStart - w.Position;
            //if (sectionPadding < 0) throw new FileFormatException("Stream position (potentially after writing sections) is behind item start. After opening a TeaFile and reading the header, the file pointer must be exactly at the begin of the item area.");
            //w.WriteZeroBytes((int) sectionPadding);

            return wc;
        }
        /// <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();
            }
        }