|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.IO.Pipelines; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Quick.Protocol.Streams |
| 12 | +{ |
| 13 | + public class ReadOnlySequenceByteStream : Stream |
| 14 | + { |
| 15 | + private ReadOnlySequence<byte> _sequence; |
| 16 | + |
| 17 | + public override bool CanRead => true; |
| 18 | + |
| 19 | + public override bool CanSeek => false; |
| 20 | + |
| 21 | + public override bool CanWrite => false; |
| 22 | + |
| 23 | + public override long Length => _sequence.Length; |
| 24 | + |
| 25 | + public override long Position |
| 26 | + { |
| 27 | + get |
| 28 | + { |
| 29 | + throw new NotSupportedException(); |
| 30 | + } |
| 31 | + set |
| 32 | + { |
| 33 | + throw new NotSupportedException(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public ReadOnlySequenceByteStream(ReadOnlySequence<byte> sequence) |
| 39 | + { |
| 40 | + _sequence = sequence; |
| 41 | + } |
| 42 | + |
| 43 | + public override int Read(byte[] buffer, int offset, int count) |
| 44 | + { |
| 45 | + if (buffer == null) |
| 46 | + { |
| 47 | + throw new ArgumentNullException(nameof(buffer)); |
| 48 | + } |
| 49 | + |
| 50 | + return ReadInternal(new Span<byte>(buffer, offset, count)); |
| 51 | + } |
| 52 | + |
| 53 | + public override int ReadByte() |
| 54 | + { |
| 55 | + Span<byte> buffer = stackalloc byte[1]; |
| 56 | + if (ReadInternal(buffer) != 0) |
| 57 | + { |
| 58 | + return buffer[0]; |
| 59 | + } |
| 60 | + |
| 61 | + return -1; |
| 62 | + } |
| 63 | + |
| 64 | + private int ReadInternal(Span<byte> buffer) |
| 65 | + { |
| 66 | + var count = Math.Min((int)_sequence.Length, buffer.Length); |
| 67 | + _sequence.Slice(0, count).CopyTo(buffer); |
| 68 | + _sequence = _sequence.Slice(count); |
| 69 | + return count; |
| 70 | + } |
| 71 | + |
| 72 | + public override long Seek(long offset, SeekOrigin origin) |
| 73 | + { |
| 74 | + throw new NotSupportedException(); |
| 75 | + } |
| 76 | + |
| 77 | + public override void SetLength(long value) |
| 78 | + { |
| 79 | + throw new NotSupportedException(); |
| 80 | + } |
| 81 | + |
| 82 | + public override void Write(byte[] buffer, int offset, int count) |
| 83 | + { |
| 84 | + throw new NotSupportedException(); |
| 85 | + } |
| 86 | + |
| 87 | + public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 88 | + { |
| 89 | + if (buffer == null) |
| 90 | + { |
| 91 | + throw new ArgumentNullException(nameof(buffer)); |
| 92 | + } |
| 93 | + return Task.Run(() => ReadInternal(new Span<byte>(buffer, offset, count))); |
| 94 | + } |
| 95 | + |
| 96 | + public override int Read(Span<byte> buffer) |
| 97 | + { |
| 98 | + return ReadInternal(buffer); |
| 99 | + } |
| 100 | + |
| 101 | + public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(CancellationToken)) |
| 102 | + { |
| 103 | + return new ValueTask<int>(Task.Run(() => ReadInternal(buffer.Span))); |
| 104 | + } |
| 105 | + |
| 106 | + public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) |
| 107 | + { |
| 108 | + var buffer = new byte[bufferSize]; |
| 109 | + while (_sequence.Length > 0) |
| 110 | + { |
| 111 | + var count = Math.Min((int)_sequence.Length, bufferSize); |
| 112 | + _sequence.Slice(0, count).CopyTo(buffer); |
| 113 | + _sequence = _sequence.Slice(count); |
| 114 | + await destination.WriteAsync(buffer, 0, count).ConfigureAwait(false); |
| 115 | + } |
| 116 | + await destination.FlushAsync().ConfigureAwait(false); |
| 117 | + } |
| 118 | + |
| 119 | + public override void Flush() |
| 120 | + { |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments