/// <summary> /// Creates a new instance of StringPacker. /// </summary> /// <param name="strings">The strings to add.</param> public StringPacker(params string[] strings) { int totallength = strings.Length + 1; foreach (string str in strings) totallength += str.Length; stringPacket = new DataPacket(new byte[totallength]); stringPacket.WriteByte((byte)strings.Length, 0); int nextoffset = 1; foreach (string str in strings) { stringPacket.WriteStringWithLength(str, nextoffset, out nextoffset); } }
/// <summary> /// Adds a string to the packet. /// </summary> /// <param name="str">The string to add.</param> public void AddString(string str) { DataPacket expanded = new DataPacket(new byte[Size + str.Length + 1]); // creates a new packet expanded.WriteByte((byte)(stringPacket.ReadByte(0) + 1), 0); // sets a new string packet size expanded.WriteBytes(stringPacket.Copy(), 0); // copies all the current strings if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str)) expanded.WriteByte(0, Size); else expanded.WriteStringWithLength(str, Size); // writes the new string at the end stringPacket.Dispose(); // disposes the old packet stringPacket = expanded; // sets the old packet to be the new }