| | | 1 | | /******************************************************************************** |
| | | 2 | | * Base64BodyWriterStream.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Buffers; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | |
| | | 13 | | namespace NanoRoute.AwsLambda |
| | | 14 | | { |
| | | 15 | | internal sealed class Base64BodyWriterStream : Stream |
| | | 16 | | { |
| | | 17 | | #region Private |
| | 1 | 18 | | private static readonly ArrayPool<char> s_arrayPool = ArrayPool<char>.Create(); |
| | | 19 | | |
| | 1 | 20 | | private StringBuilder _body = new(); |
| | 1 | 21 | | private byte[] _stagedBytes = new byte[3]; |
| | 1 | 22 | | private char[] _encodedChars = s_arrayPool.Rent(4096); |
| | | 23 | | |
| | | 24 | | private int _stagedByteCount; |
| | | 25 | | |
| | | 26 | | private void AppendEncoded(ReadOnlySpan<byte> bytes, CancellationToken cancellation) |
| | 1 | 27 | | { |
| | 1 | 28 | | while (!bytes.IsEmpty) |
| | 1 | 29 | | { |
| | 1 | 30 | | cancellation.ThrowIfCancellationRequested(); |
| | | 31 | | |
| | 1 | 32 | | int bytesToEncode = Math.Min(bytes.Length, (_encodedChars.Length / 4) * 3); |
| | | 33 | | |
| | 1 | 34 | | if (!Convert.TryToBase64Chars(bytes.Slice(0, bytesToEncode), _encodedChars, out int charsWritten)) |
| | 0 | 35 | | throw new InvalidOperationException(); |
| | | 36 | | |
| | 1 | 37 | | _body.Append(_encodedChars, 0, charsWritten); |
| | 1 | 38 | | bytes = bytes.Slice(bytesToEncode); |
| | 1 | 39 | | } |
| | 1 | 40 | | } |
| | | 41 | | |
| | 1 | 42 | | private void EnsureNotDisposed() => ObjectDisposedException.ThrowIf(_body is null, this); |
| | | 43 | | |
| | | 44 | | private void WriteCore(ReadOnlySpan<byte> buffer, CancellationToken cancellation) |
| | 1 | 45 | | { |
| | 1 | 46 | | EnsureNotDisposed(); |
| | | 47 | | |
| | 1 | 48 | | cancellation.ThrowIfCancellationRequested(); |
| | | 49 | | |
| | 1 | 50 | | if (buffer.IsEmpty) |
| | 1 | 51 | | return; |
| | | 52 | | |
| | 1 | 53 | | if (_stagedByteCount > 0) |
| | 1 | 54 | | { |
| | 1 | 55 | | int bytesToStage = Math.Min(_stagedBytes.Length - _stagedByteCount, buffer.Length); |
| | | 56 | | |
| | 1 | 57 | | buffer.Slice(0, bytesToStage).CopyTo(_stagedBytes.AsSpan(_stagedByteCount)); |
| | 1 | 58 | | _stagedByteCount += bytesToStage; |
| | 1 | 59 | | buffer = buffer.Slice(bytesToStage); |
| | | 60 | | |
| | 1 | 61 | | if (_stagedByteCount < _stagedBytes.Length) |
| | 1 | 62 | | return; |
| | | 63 | | |
| | 1 | 64 | | AppendEncoded(_stagedBytes, cancellation); |
| | 1 | 65 | | _stagedByteCount = 0; |
| | 1 | 66 | | } |
| | | 67 | | |
| | 1 | 68 | | int blockLength = buffer.Length - (buffer.Length % 3); |
| | | 69 | | |
| | 1 | 70 | | if (blockLength > 0) |
| | 1 | 71 | | { |
| | 1 | 72 | | AppendEncoded(buffer.Slice(0, blockLength), cancellation); |
| | 1 | 73 | | buffer = buffer.Slice(blockLength); |
| | 1 | 74 | | } |
| | | 75 | | |
| | 1 | 76 | | if (!buffer.IsEmpty) |
| | 1 | 77 | | { |
| | 1 | 78 | | buffer.CopyTo(_stagedBytes); |
| | 1 | 79 | | _stagedByteCount = buffer.Length; |
| | 1 | 80 | | } |
| | 1 | 81 | | } |
| | | 82 | | |
| | | 83 | | protected override void Dispose(bool disposing) |
| | 1 | 84 | | { |
| | 1 | 85 | | _body = null!; |
| | 1 | 86 | | _stagedBytes = null!; |
| | | 87 | | |
| | 1 | 88 | | s_arrayPool.Return(_encodedChars, clearArray: false); |
| | 1 | 89 | | _encodedChars = null!; |
| | | 90 | | |
| | 1 | 91 | | base.Dispose(disposing); |
| | 1 | 92 | | } |
| | | 93 | | #endregion |
| | | 94 | | |
| | | 95 | | public override bool CanRead { get; } |
| | | 96 | | |
| | | 97 | | public override bool CanSeek { get; } |
| | | 98 | | |
| | 1 | 99 | | public override bool CanWrite { get; } = true; |
| | | 100 | | |
| | | 101 | | public string GetBody() |
| | 1 | 102 | | { |
| | 1 | 103 | | EnsureNotDisposed(); |
| | | 104 | | |
| | 1 | 105 | | if (_stagedByteCount is 0) |
| | 1 | 106 | | return _body.ToString(); |
| | | 107 | | |
| | 1 | 108 | | if (!Convert.TryToBase64Chars(_stagedBytes.AsSpan(0, _stagedByteCount), _encodedChars, out int charsWritten) |
| | 0 | 109 | | throw new InvalidOperationException(); |
| | | 110 | | |
| | 1 | 111 | | return string.Concat |
| | 1 | 112 | | ( |
| | 1 | 113 | | _body.ToString(), |
| | 1 | 114 | | new string(_encodedChars, 0, charsWritten) |
| | 1 | 115 | | ); |
| | 1 | 116 | | } |
| | | 117 | | |
| | | 118 | | public override void Write(byte[] buffer, int offset, int count) |
| | 1 | 119 | | { |
| | 1 | 120 | | ArgumentNullException.ThrowIfNull(buffer); |
| | | 121 | | |
| | 1 | 122 | | if (offset < 0 || offset > buffer.Length) |
| | 1 | 123 | | throw new ArgumentOutOfRangeException(nameof(offset)); |
| | | 124 | | |
| | 1 | 125 | | if (count < 0 || count > buffer.Length - offset) |
| | 1 | 126 | | throw new ArgumentOutOfRangeException(nameof(count)); |
| | | 127 | | |
| | 1 | 128 | | WriteCore(buffer.AsSpan(offset, count), CancellationToken.None); |
| | 1 | 129 | | } |
| | | 130 | | |
| | | 131 | | public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellation) |
| | | 132 | | { |
| | | 133 | | await Task.Yield(); |
| | | 134 | | |
| | | 135 | | WriteCore(buffer.AsSpan(offset, count), cancellation); |
| | | 136 | | } |
| | | 137 | | |
| | 1 | 138 | | public override void Write(ReadOnlySpan<byte> buffer) => WriteCore(buffer, CancellationToken.None); |
| | | 139 | | |
| | | 140 | | public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellation) |
| | | 141 | | { |
| | | 142 | | await Task.Yield(); |
| | | 143 | | |
| | | 144 | | WriteCore(buffer.Span, cancellation); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | #region Not Supported members |
| | 1 | 148 | | public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| | | 149 | | |
| | 1 | 150 | | public override void SetLength(long value) => throw new NotSupportedException(); |
| | | 151 | | |
| | 1 | 152 | | public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| | | 153 | | |
| | 1 | 154 | | public override long Length => throw new NotSupportedException(); |
| | | 155 | | |
| | | 156 | | public override long Position |
| | | 157 | | { |
| | 1 | 158 | | get => throw new NotSupportedException(); |
| | 1 | 159 | | set => throw new NotSupportedException(); |
| | | 160 | | } |
| | | 161 | | |
| | 1 | 162 | | public override void Flush() { } |
| | | 163 | | |
| | 1 | 164 | | public override Task FlushAsync(CancellationToken cancellation) => Task.CompletedTask; |
| | | 165 | | #endregion |
| | | 166 | | } |
| | | 167 | | } |