/// <summary> /// Initializes a new instance of the <code>ElementDescriptor</code> class. /// </summary> /// <param name="identifier">the element identifier</param> /// <param name="name">the element name or <code>null</code> if the name is not known</param> /// <param name="type">the element type or <code>null</code> if the type is not known</param> /// <exception cref="ArgumentNullException">if <code>identifier</code> is <code>null</code></exception> public ElementDescriptor(VInt identifier, String name, ElementType type) { if (!identifier.IsValidIdentifier) throw new ArgumentException("Value is not valid identifier", "identifier"); _identifier = identifier; _name = name; _type = type; }
internal MasterBlockWriter(EbmlWriter writer, VInt elementId) : base(new MemoryStream()) { var flushed = false; _flushImpl = () => { if (flushed) return; var data = ((MemoryStream) _stream).ToArray(); writer.Write(elementId, data); flushed = true; _stream.Close(); }; }
/// <summary> /// Starts nested stream. Upon disposal of nested stream, the size is calculated and written to nested element header. /// Example: /// using(var data = writer.StartMasterElement(InnerDataElementId)) /// { /// data.WriteInt(DataItem1Id, 139874) /// data.WriteUtf(DataItem2Id, "Hello world"); /// } /// </summary> /// <param name="elementId"></param> /// <returns></returns> public MasterBlockWriter StartMasterElement(VInt elementId) { return new MasterBlockWriter(this, elementId); }
/// <summary> /// Writes string in UTF-8 encoding /// </summary> /// <param name="elementId"></param> /// <param name="value"></param> /// <returns></returns> public int WriteUtf(VInt elementId, string value) { if (value == null) throw new ArgumentNullException("value"); if (!value.IsNormalized(NormalizationForm.FormC)) { value = value.Normalize(NormalizationForm.FormC); } var buffer = string.IsNullOrEmpty(value) ? new byte[0] : Encoding.UTF8.GetBytes(value); return Write(elementId, buffer, 0, buffer.Length); }
/// <summary> /// Writes element header /// </summary> /// <param name="elementId"></param> /// <param name="size"></param> /// <returns></returns> public int WriteElementHeader(VInt elementId, VInt size) { return elementId.Write(_stream) + size.Write(_stream); }
/// <summary> /// Writes string in ASCII encoding /// </summary> /// <param name="elementId"></param> /// <param name="value"></param> /// <returns></returns> public int WriteAscii(VInt elementId, string value) { if (value == null) throw new ArgumentNullException("value"); var buffer = string.IsNullOrEmpty(value) ? new byte[0] : Encoding.ASCII.GetBytes(value); return Write(elementId, buffer, 0, buffer.Length); }
/// <summary> /// Writes binary data /// </summary> /// <param name="elementId"></param> /// <param name="data"></param> /// <returns></returns> public int Write(VInt elementId, byte[] data) { return Write(elementId, data, 0, data.Length); }
/// <summary> /// Writes binary data /// </summary> /// <param name="elementId"></param> /// <param name="data"></param> /// <returns></returns> public int Write(VInt elementId, byte[] data) { return(Write(elementId, data, 0, data.Length)); }
/// <summary> /// Writes floating point number /// </summary> /// <param name="elementId"></param> /// <param name="value"></param> /// <returns></returns> public int Write(VInt elementId, float value) { var u = new Union {fval = value}.uival; return elementId.Write(_stream) + EncodeWidth(4).Write(_stream) + WriteInt(u, 4); }
/// <summary> /// Writes datetime value /// </summary> /// <param name="elementId"></param> /// <param name="dateTime"></param> /// <returns></returns> public int Write(VInt elementId, DateTime dateTime) { var d = (dateTime - EbmlReader.MilleniumStart).Ticks * 100; return elementId.Write(_stream) + EncodeWidth(8).Write(_stream) + WriteInt(d, 8); }
/// <summary> /// Writes unsigned integer value /// </summary> /// <param name="elementId"></param> /// <param name="value"></param> /// <returns></returns> public int Write(VInt elementId, UInt64 value) { var length = 0u; for (var mask = UInt64.MaxValue; (value & mask) != 0 && length < 8; mask <<= 8) { length++; } return elementId.Write(_stream) + EncodeWidth(length).Write(_stream) + WriteUInt(value, length); }
public bool Equals(VInt other) { return other._encodedValue == _encodedValue && other._length == _length; }
/// <summary> /// Initializes a new instance of the <code>ElementDescriptor</code> class. /// </summary> /// <param name="identifier"></param> /// <param name="name"></param> /// <param name="type"></param> public ElementDescriptor(long identifier, string name, ElementType type) : this(VInt.FromEncoded((ulong)identifier), name, type) { }
/// <summary> /// Writes element header /// </summary> /// <param name="elementId"></param> /// <param name="size"></param> /// <returns></returns> public int WriteElementHeader(VInt elementId, VInt size) { return(elementId.Write(_stream) + size.Write(_stream)); }
/// <summary> /// Starts nested stream. Upon disposal of nested stream, the size is calculated and written to nested element header. /// Example: /// using(var data = writer.StartMasterElement(InnerDataElementId)) /// { /// data.WriteInt(DataItem1Id, 139874) /// data.WriteUtf(DataItem2Id, "Hello world"); /// } /// </summary> /// <param name="elementId"></param> /// <returns></returns> public MasterBlockWriter StartMasterElement(VInt elementId) { return(new MasterBlockWriter(this, elementId)); }
/// <summary> /// Writes signed integer value /// </summary> /// <param name="elementId"></param> /// <param name="value"></param> /// <returns></returns> public int Write(VInt elementId, Int64 value) { var length = value == 0 ? 0u : 1u; unchecked { var mask = (long)0xffffffffffffff80L; for (var expected = value < 0L ? mask : 0L; length < 8 && (mask & value) != expected; mask <<= 8, expected <<= 8) { length++; } } return elementId.Write(_stream) + EncodeWidth(length).Write(_stream) + WriteInt(value, length); }
public Element(VInt identifier, long sizeValue, ElementType type) { Identifier = identifier; Size = sizeValue; Remaining = sizeValue; Type = type; }
/// <summary> /// Writes floating point number with double precision /// </summary> /// <param name="elementId"></param> /// <param name="value"></param> /// <returns></returns> public int Write(VInt elementId, double value) { var u = new Union { dval = value }.ulval; return elementId.Write(_stream) + EncodeWidth(8).Write(_stream) + WriteUInt(u, 8); }
/// <summary> /// Writes binary data /// </summary> /// <param name="elementId"></param> /// <param name="data"></param> /// <param name="offset"></param> /// <param name="length"></param> /// <returns></returns> public int Write(VInt elementId, byte[] data, int offset, int length) { var headLen = elementId.Write(_stream) + EncodeWidth((uint) length).Write(_stream); _stream.Write(data, offset, length); return headLen + length; }
/// <summary> /// Writes datetime value /// </summary> /// <param name="elementId"></param> /// <param name="dateTime"></param> /// <returns></returns> public int Write(VInt elementId, DateTime dateTime) { var d = (dateTime - EbmlReader.MilleniumStart).Ticks * 100; return(elementId.Write(_stream) + EncodeWidth(8).Write(_stream) + WriteInt(d, 8)); }