/// <summary> /// Writes a Subtitle Packet to a Stream. /// </summary> /// <param name="Writer">The writer of the output Stream</param> /// <param name="Packet">The packet to be written</param> public static void ToStream(EndianBinaryWriter Writer, SubtitlePacket Packet) { using (MemoryStream Content = new MemoryStream()) { EndianBinaryWriter CWriter = new EndianBinaryWriter(Content, Writer.Endian); foreach (SubtitlePacketText Text in Packet.Texts) { SubtitlePacketText.ToStream(CWriter, Text); } int Length = (int)Content.Length + 0x14 + 1; if ((Length & 0xf) != 0) { Length = (Length & ~0xf) + 0x10; } Writer.Endian = Endian.Little; Writer.Write(0u); Writer.Write(Length); Writer.Write(Packet.BaseStartTime); Writer.Endian = Endian.Big; Writer.Write(0u); Writer.Write((int)Content.Length); Writer.Write(Content.ToArray()); Writer.Write((byte)0); while ((Writer.BaseStream.Position & 0xf) != 0) { Writer.Write((byte)0); } } }
/// <summary> /// Reads a Subtitle Packet from a Stream. /// </summary> /// <param name="Reader">The reader of the Stream where the data is located</param> /// <returns>The packet as a object</returns> public static SubtitlePacket FromStream(EndianBinaryReader Reader) { SubtitlePacket Packet = new SubtitlePacket(); long BasePosition = Reader.BaseStream.Position; Reader.Endian = Endian.Little; uint Signature = Reader.ReadUInt32(); uint PacketLength = Reader.ReadUInt32(); long EndPosition = BasePosition + PacketLength; if (PacketLength == 0 || Signature != 0) { return(null); } Packet.BaseStartTime = Reader.ReadUInt32(); Reader.Endian = Endian.Big; uint Dummy = Reader.ReadUInt32(); uint DataLength = Reader.ReadUInt32(); while (Reader.BaseStream.Position + 0x10 < EndPosition) { Packet.Texts.Add(SubtitlePacketText.FromStream(Reader)); } Reader.Seek(EndPosition, SeekOrigin.Begin); return(Packet); }
/// <summary> /// Writes a Subtitle Packet to a Byte Array. /// </summary> /// <param name="Packet">The packet to be written</param> /// <returns>The array with the data</returns> public static byte[] ToBytes(SubtitlePacket Packet) { using (MemoryStream Output = new MemoryStream()) { EndianBinaryWriter Writer = new EndianBinaryWriter(Output, Endian.Big); ToStream(Writer, Packet); return(Output.ToArray()); } }