public void SizePrefixedBytesWorks() { var pw = new PaddedWrapper(); var data1 = new byte[10]; var data2 = new byte[10]; Array.Fill(data1, (byte)0xFF); Array.Fill(data2, (byte)0x11); var spb = new SizePrefixedBytes(); var spb2 = new SizePrefixedBytes(); spb.data = new byte[10 + sizeof(int)]; spb2.data = new byte[10 + sizeof(int)]; BitConverter.TryWriteBytes(spb.data, 10); BitConverter.TryWriteBytes(spb2.data, 10); Array.Copy(data1, 0, spb.data, sizeof(int), data1.Length); Array.Copy(data2, 0, spb2.data, sizeof(int), data1.Length); if (!pw.CanPackIn(spb2.data)) { pw = pw.CopyToBigger(spb2.data.Length); } pw.initFrom(spb); pw.PackIn(spb2.data); }
public static unsafe SizePrefixedBytes ToSizePrefixedBytes(this Map attrs) { var maxLen = Map.Serializer.GetMaxSize(attrs); // don't stack alloc if it's big Span <byte> buff = maxLen > (0x10000) ?new byte[maxLen + sizeof(int)] : stackalloc byte[maxLen + sizeof(int)]; var actSize = Map.Serializer.Write(buff.Slice(sizeof(int)), attrs); MemoryMarshal.Write(buff, ref actSize); var spb = new SizePrefixedBytes(); spb.data = buff.Slice(0, actSize + sizeof(int)).ToArray(); return(spb); }