public int Serialize(ref byte[] bytes, int offset, RenderBatch value, IFormatterResolver formatterResolver) { using (var memoryStream = new MemoryStream()) using (var renderBatchWriter = new RenderBatchWriter(memoryStream, leaveOpen: false)) { renderBatchWriter.Write(value); var bytesBuffer = memoryStream.GetBuffer(); return(MessagePackBinary.WriteBytes(ref bytes, offset, bytesBuffer, 0, (int)memoryStream.Length)); } }
public int Serialize(ref byte[] bytes, int offset, RenderBatch value, IFormatterResolver formatterResolver) { // Instead of using MessagePackBinary.WriteBytes, we write into a stream that // knows how to write the data using MessagePack writer APIs. The benefit // is that we don't have to allocate a second large buffer to capture the // RenderBatchWriter output - we can just write directly to the underlying // output buffer. using (var bufferStream = new MessagePackBufferStream(bytes, offset)) using (var renderBatchWriter = new RenderBatchWriter(bufferStream, leaveOpen: false)) { renderBatchWriter.Write(value); bytes = bufferStream.Buffer; // In case the buffer was expanded return((int)bufferStream.Length); } }