|
1 | | -using Microsoft.JSInterop; |
| 1 | +using KristofferStrube.Blazor.WebIDL; |
| 2 | +using Microsoft.JSInterop; |
2 | 3 |
|
3 | 4 | namespace KristofferStrube.Blazor.Streams; |
4 | 5 |
|
@@ -44,17 +45,53 @@ public override void Write(byte[] buffer, int offset, int count) |
44 | 45 | throw new InvalidOperationException($"Writing to {nameof(ReadableStream)} is not supported as it is meant for reading."); |
45 | 46 | } |
46 | 47 |
|
| 48 | + private byte[]? additionalDataRead; |
| 49 | + |
47 | 50 | public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
48 | 51 | { |
| 52 | + // If there is more than the desired buffer size available from previous reads then use those bytes. |
| 53 | + if (additionalDataRead?.Length > buffer.Length) |
| 54 | + { |
| 55 | + additionalDataRead[..buffer.Length].CopyTo(buffer); |
| 56 | + additionalDataRead = additionalDataRead[buffer.Length..]; |
| 57 | + return buffer.Length; |
| 58 | + } |
| 59 | + |
| 60 | + // If there is exactly enough data from previous reads, then use those bytes and clear the old data. |
| 61 | + if (additionalDataRead?.Length == buffer.Length) |
| 62 | + { |
| 63 | + additionalDataRead.CopyTo(buffer); |
| 64 | + additionalDataRead = null; |
| 65 | + return buffer.Length; |
| 66 | + } |
| 67 | + |
| 68 | + // There is some data left from previous reads but not enough to fill the buffer. |
| 69 | + int bytesCopiedFromExcessDataReadPreviously = 0; |
| 70 | + if (additionalDataRead is not null) |
| 71 | + { |
| 72 | + additionalDataRead.CopyTo(buffer); |
| 73 | + bytesCopiedFromExcessDataReadPreviously = additionalDataRead.Length; |
| 74 | + additionalDataRead = null; |
| 75 | + } |
| 76 | + |
49 | 77 | reader ??= await GetDefaultReaderAsync(); |
50 | 78 | ReadableStreamReadResult read = await reader.ReadAsync(); |
51 | 79 | if (!await read.GetDoneAsync()) |
52 | 80 | { |
53 | 81 | IJSObjectReference jSValue = await read.GetValueAsync(); |
54 | | - IJSObjectReference helper = await helperTask.Value; |
55 | | - int length = await helper.InvokeAsync<int>("getAttribute", jSValue, "length"); |
56 | | - (await helper.InvokeAsync<byte[]>("byteArray", jSValue)).CopyTo(buffer); |
57 | | - return length; |
| 82 | + await using Uint8Array value = await Uint8Array.CreateAsync(JSRuntime, jSValue); |
| 83 | + byte[] data = await value.GetAsArrayAsync(); |
| 84 | + int bytesNeededToFillBuffer = buffer.Length - bytesCopiedFromExcessDataReadPreviously; |
| 85 | + if (data.Length > bytesNeededToFillBuffer) |
| 86 | + { |
| 87 | + data[..bytesNeededToFillBuffer].CopyTo(buffer[bytesCopiedFromExcessDataReadPreviously..]); |
| 88 | + additionalDataRead = data[bytesNeededToFillBuffer..]; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + data.CopyTo(buffer[bytesCopiedFromExcessDataReadPreviously..]); |
| 93 | + } |
| 94 | + return Math.Min(data.Length, buffer.Length); |
58 | 95 | } |
59 | 96 | await reader.ReleaseLockAsync(); |
60 | 97 | reader = null; |
|
0 commit comments