public void Process(Stream source, Stream target) { var bufferLength = ReadBlockLength(source); var buffer = new Block <byte>(bufferLength); try { for (var read = source.Read(buffer.Memory.Span); read > 0; read = source.Read(buffer.Memory.Span)) { using (var processed = _operation.Process(buffer.Memory.Span)) { target.Write(processed.Memory.Span); } bufferLength = ReadBlockLength(source); if (bufferLength == 0) { break; } buffer.Dispose(); buffer = new Block <byte>(bufferLength); } } finally { buffer.Dispose(); } }
public void Process(Stream source, Stream target) { using var block = new Block <byte>(4096); Span <byte> lengthBuffer = stackalloc byte[sizeof(int)]; for (var read = source.Read(block.Memory.Span); read > 0; read = source.Read(block.Memory.Span)) { using var processed = _operation.Process(block.Memory.Slice(0, read).Span); var blockLength = processed.Memory.Length; lengthBuffer.Clear(); BitConverter.TryWriteBytes(lengthBuffer, blockLength); target.Write(lengthBuffer); target.Write(processed.Memory.Span); } }