You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe the problem.
the PullFromJSDataStream can be marked as seekable because the underlying js interop uses blob slicing.
Describe the solution you'd like
the class below is just a copy-paste from the existing PullFromJSDataStream code. i tested this with a File from input of type file on blazor wasm.
classWebStream:Stream{privatereadonlyIJSRuntime_runtime;privatereadonlyIJSStreamReference_jsStreamReference;privatereadonlylong_totalLength;privatereadonlyCancellationToken_streamCancellationToken;privatelong_offset;publicstaticWebStreamCreateWebStream(IJSRuntimeruntime,IJSStreamReferencejsStreamReference,longtotalLength,CancellationTokencancellationToken=default){returnnewWebStream(runtime,jsStreamReference,totalLength,cancellationToken);}privateWebStream(IJSRuntimeruntime,IJSStreamReferencejsStreamReference,longtotalLength,CancellationTokencancellationToken){_runtime=runtime;_jsStreamReference=jsStreamReference;_totalLength=totalLength;_streamCancellationToken=cancellationToken;_offset=0;}publicoverrideboolCanRead=>true;publicoverrideboolCanSeek=>true;publicoverrideboolCanWrite=>false;publicoverridelongLength=>_totalLength;publicoverridelongPosition{get=>_offset;set=>Seek(value,SeekOrigin.Begin);}publicoverridevoidFlush(){// No-op}publicoverrideTaskFlushAsync(CancellationTokencancellationToken)=>Task.CompletedTask;publicoverrideintRead(byte[]buffer,intoffset,intcount)=>thrownewNotSupportedException("Synchronous reads are not supported.");publicoverridelongSeek(longoffset,SeekOriginorigin){varnewOffset=originswitch{SeekOrigin.Begin=>offset,SeekOrigin.Current=>_offset+offset,SeekOrigin.End=>_totalLength+offset,
_ =>thrownewArgumentOutOfRangeException(nameof(origin),origin,null),};if(newOffset<0||newOffset>_totalLength){thrownewArgumentOutOfRangeException(nameof(offset),"Seek offset is out of bounds.");}_offset=newOffset;return_offset;}publicoverridevoidSetLength(longvalue)=>thrownewNotSupportedException();publicoverridevoidWrite(byte[]buffer,intoffset,intcount)=>thrownewNotSupportedException();publicoverrideasyncTask<int>ReadAsync(byte[]buffer,intoffset,intcount,CancellationTokencancellationToken)=>awaitReadAsync(buffer.AsMemory(offset,count),cancellationToken);publicoverrideasyncValueTask<int>ReadAsync(Memory<byte>buffer,CancellationTokencancellationToken=default){varbytesRead=awaitRequestDataFromJSAsync(buffer.Length);ThrowIfCancellationRequested(cancellationToken);bytesRead.CopyTo(buffer);returnbytesRead.Length;}privatevoidThrowIfCancellationRequested(CancellationTokencancellationToken){if(cancellationToken.IsCancellationRequested||_streamCancellationToken.IsCancellationRequested){thrownewTaskCanceledException();}}privateasyncValueTask<byte[]>RequestDataFromJSAsync(intnumBytesToRead){numBytesToRead=(int)Math.Min(numBytesToRead,_totalLength-_offset);varbytesRead=await_runtime.InvokeAsync<byte[]>("Blazor._internal.getJSDataStreamChunk",_jsStreamReference,_offset,numBytesToRead);if(bytesRead.Length!=numBytesToRead){thrownewEndOfStreamException("Failed to read the requested number of bytes from the stream.");}_offset+=bytesRead.Length;if(_offset==_totalLength){Dispose(true);}returnbytesRead;}}
Additional context
No response
The text was updated successfully, but these errors were encountered:
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
the
PullFromJSDataStream
can be marked as seekable because the underlying js interop uses blob slicing.Describe the solution you'd like
the class below is just a copy-paste from the existing
PullFromJSDataStream
code. i tested this with aFile
frominput
of typefile
on blazor wasm.Additional context
No response
The text was updated successfully, but these errors were encountered: