public async Task Execute(IReadableChannel input, IWritableChannel output) { while (true) { var inputBuffer = await input; if (inputBuffer.IsEmpty && input.Completion.IsCompleted) { break; } var writerBuffer = output.Alloc(2048); var span = inputBuffer.FirstSpan; _deflater.SetInput(span.BufferPtr, span.Length); while (!_deflater.NeedsInput()) { int written = _deflater.ReadDeflateOutput(writerBuffer.Memory.BufferPtr, writerBuffer.Memory.Length); writerBuffer.CommitBytes(written); } var consumed = span.Length - _deflater.AvailableInput; inputBuffer = inputBuffer.Slice(0, consumed); inputBuffer.Consumed(); await writerBuffer.FlushAsync(); } bool flushed; do { // Need to do more stuff here var writerBuffer = output.Alloc(2048); int compressedBytes; flushed = _deflater.Flush(writerBuffer.Memory.BufferPtr, writerBuffer.Memory.Length, out compressedBytes); writerBuffer.CommitBytes(compressedBytes); await writerBuffer.FlushAsync(); }while (flushed); bool finished; do { // Need to do more stuff here var writerBuffer = output.Alloc(2048); int compressedBytes; finished = _deflater.Finish(writerBuffer.Memory.BufferPtr, writerBuffer.Memory.Length, out compressedBytes); writerBuffer.CommitBytes(compressedBytes); await writerBuffer.FlushAsync(); }while (!finished); input.CompleteReading(); output.CompleteWriting(); _deflater.Dispose(); }
public async Task Execute(IReadableChannel input, IWritableChannel output) { while (true) { var inputBuffer = await input.ReadAsync(); if (inputBuffer.IsEmpty && input.Reading.IsCompleted) { break; } var writerBuffer = output.Alloc(2048); var memory = inputBuffer.First; unsafe { _deflater.SetInput((IntPtr)memory.UnsafePointer, memory.Length); } while (!_deflater.NeedsInput()) { unsafe { int written = _deflater.ReadDeflateOutput((IntPtr)writerBuffer.Memory.UnsafePointer, writerBuffer.Memory.Length); writerBuffer.Advance(written); } } var consumed = memory.Length - _deflater.AvailableInput; inputBuffer = inputBuffer.Slice(0, consumed); input.Advance(inputBuffer.End); await writerBuffer.FlushAsync(); } bool flushed; do { // Need to do more stuff here var writerBuffer = output.Alloc(2048); var memory = writerBuffer.Memory; unsafe { int compressedBytes; flushed = _deflater.Flush((IntPtr)memory.UnsafePointer, memory.Length, out compressedBytes); writerBuffer.Advance(compressedBytes); } await writerBuffer.FlushAsync(); }while (flushed); bool finished; do { // Need to do more stuff here var writerBuffer = output.Alloc(2048); var memory = writerBuffer.Memory; unsafe { int compressedBytes; finished = _deflater.Finish((IntPtr)memory.UnsafePointer, memory.Length, out compressedBytes); writerBuffer.Advance(compressedBytes); } await writerBuffer.FlushAsync(); }while (!finished); input.Complete(); output.Complete(); _deflater.Dispose(); }