@@ -20,6 +20,8 @@ public partial class FileReaderJsInterop
2020 private static readonly Dictionary < long , TaskCompletionSource < int > > _readFileUnmarshalledCalls
2121 = new Dictionary < long , TaskCompletionSource < int > > ( ) ;
2222
23+
24+
2325 internal IJSRuntime CurrentJSRuntime ;
2426 internal IJSUnmarshalledRuntime UnmarshalledRuntime ;
2527
@@ -53,25 +55,25 @@ internal void Initialize()
5355 internal async Task < bool > RegisterDropEvents ( ElementReference elementReference , DropEventsOptions options )
5456 {
5557 await EnsureInitializedAsync ( ) ;
56- return await CurrentJSRuntime . InvokeAsync < bool > ( $ " FileReaderComponent.RegisterDropEvents" , elementReference , options ) ;
58+ return await CurrentJSRuntime . InvokeAsync < bool > ( FileReaderComponent . RegisterDropEvents , elementReference , options ) ;
5759 }
5860
5961 public async Task < bool > UnregisterDropEvents ( ElementReference elementReference )
6062 {
6163 await EnsureInitializedAsync ( ) ;
62- return await CurrentJSRuntime . InvokeAsync < bool > ( $ " FileReaderComponent.UnregisterDropEvents" , elementReference ) ;
64+ return await CurrentJSRuntime . InvokeAsync < bool > ( FileReaderComponent . UnregisterDropEvents , elementReference ) ;
6365 }
6466
6567 internal async Task < bool > RegisterPasteEvent ( ElementReference elementReference , PasteEventOptions options )
6668 {
6769 await EnsureInitializedAsync ( ) ;
68- return await CurrentJSRuntime . InvokeAsync < bool > ( $ " FileReaderComponent.RegisterPasteEvent" , elementReference , options ) ;
70+ return await CurrentJSRuntime . InvokeAsync < bool > ( FileReaderComponent . RegisterPasteEvent , elementReference , options ) ;
6971 }
7072
7173 public async Task < bool > UnregisterPasteEvent ( ElementReference elementReference )
7274 {
7375 await EnsureInitializedAsync ( ) ;
74- return await CurrentJSRuntime . InvokeAsync < bool > ( $ " FileReaderComponent.UnregisterPasteEvent" , elementReference ) ;
76+ return await CurrentJSRuntime . InvokeAsync < bool > ( FileReaderComponent . UnregisterPasteEvent , elementReference ) ;
7577 }
7678
7779 public async Task < AsyncDisposableStream > OpenFileStream ( ElementReference elementReference , int index , IFileInfo fileInfo )
@@ -87,18 +89,18 @@ public async Task<IBase64Stream> OpenBase64Stream(ElementReference elementRefere
8789 public async Task < int > GetFileCount ( ElementReference elementReference )
8890 {
8991 await EnsureInitializedAsync ( ) ;
90- return ( int ) await CurrentJSRuntime . InvokeAsync < long > ( $ " FileReaderComponent.GetFileCount" , elementReference ) ;
92+ return ( int ) await CurrentJSRuntime . InvokeAsync < long > ( FileReaderComponent . GetFileCount , elementReference ) ;
9193 }
9294
9395 public async Task < int > ClearValue ( ElementReference elementReference )
9496 {
9597 await EnsureInitializedAsync ( ) ;
96- return ( int ) await CurrentJSRuntime . InvokeAsync < long > ( $ " FileReaderComponent.ClearValue" , elementReference ) ;
98+ return ( int ) await CurrentJSRuntime . InvokeAsync < long > ( FileReaderComponent . ClearValue , elementReference ) ;
9799 }
98100
99101 public async Task < IFileInfo > GetFileInfoFromElement ( ElementReference elementReference , int index )
100102 {
101- return await CurrentJSRuntime . InvokeAsync < FileInfo > ( $ " FileReaderComponent.GetFileInfoFromElement" , elementReference , index ) ;
103+ return await CurrentJSRuntime . InvokeAsync < FileInfo > ( FileReaderComponent . GetFileInfoFromElement , elementReference , index ) ;
102104 }
103105
104106 public async Task EnsureInitializedAsync ( bool force = false )
@@ -113,19 +115,23 @@ public async Task EnsureInitializedAsync(bool force = false)
113115
114116 private async Task < int > OpenReadAsync ( ElementReference elementReference , int fileIndex )
115117 {
116- return ( int ) await CurrentJSRuntime . InvokeAsync < long > ( $ " FileReaderComponent.OpenRead" , elementReference , fileIndex , _options . UseWasmSharedBuffer ) ;
118+ return ( int ) await CurrentJSRuntime . InvokeAsync < long > ( FileReaderComponent . OpenRead , elementReference , fileIndex , _options . UseWasmSharedBuffer ) ;
117119 }
118120
119121 private async Task < bool > DisposeStream ( int fileRef )
120122 {
121- return await CurrentJSRuntime . InvokeAsync < bool > ( $ " FileReaderComponent.Dispose" , fileRef ) ;
123+ return await CurrentJSRuntime . InvokeAsync < bool > ( FileReaderComponent . Dispose , fileRef ) ;
122124 }
123125
124126 private async Task < int > ReadFileAsync ( int fileRef , byte [ ] buffer , long position , long bufferOffset , int count , CancellationToken cancellationToken )
125127 {
126128 if ( this . _options . UseWasmSharedBuffer )
127129 {
130+ #if NET6_0_OR_GREATER
131+ return await ReadFileSliceAsync ( fileRef , buffer , position , bufferOffset , count , cancellationToken ) ;
132+ #else
128133 return await ReadFileUnmarshalledAsync ( fileRef , buffer , position , bufferOffset , count , cancellationToken ) ;
134+ #endif
129135 }
130136
131137 return await ReadFileMarshalledAsync ( fileRef , buffer , position , bufferOffset , count , cancellationToken ) ;
@@ -189,13 +195,25 @@ private async Task<string> ReadFileMarshalledBase64Async(
189195 {
190196 cancellationToken . ThrowIfCancellationRequested ( ) ;
191197 var data = await CurrentJSRuntime . InvokeAsync < string > (
192- $ " FileReaderComponent.ReadFileMarshalledAsync" , cancellationToken ,
198+ FileReaderComponent . ReadFileMarshalledAsync , cancellationToken ,
193199 new { position , count , fileRef } ) ;
194200
195201 return data ;
196202
197203 }
198204
205+ #if NET6_0_OR_GREATER
206+ private async Task < int > ReadFileSliceAsync (
207+ int fileRef , byte [ ] buffer , long position , long bufferOffset , int count ,
208+ CancellationToken cancellationToken )
209+ {
210+ await using var streamReference = await this . CurrentJSRuntime . InvokeAsync < IJSStreamReference > (
211+ FileReaderComponent . ReadFileSliceAsync , cancellationToken , fileRef , position , count ) ;
212+ using var readStream = await streamReference . OpenReadStreamAsync ( cancellationToken : cancellationToken ) ;
213+ return await readStream . ReadAsync ( buffer . AsMemory ( ( int ) bufferOffset , count ) , cancellationToken ) ;
214+ }
215+ #endif
216+
199217 private async Task < int > ReadFileUnmarshalledAsync (
200218 int fileRef , byte [ ] buffer , long position , long bufferOffset , int count ,
201219 CancellationToken cancellationToken )
@@ -207,7 +225,7 @@ private async Task<int> ReadFileUnmarshalledAsync(
207225
208226 // Buffer is not allocated here,
209227 UnmarshalledRuntime . InvokeUnmarshalled < ReadFileParams , int > (
210- $ " FileReaderComponent.ReadFileUnmarshalledAsync" ,
228+ FileReaderComponent . ReadFileUnmarshalledAsync ,
211229 new ReadFileParams {
212230 BufferOffset = bufferOffset ,
213231 Count = count ,
@@ -221,7 +239,7 @@ private async Task<int> ReadFileUnmarshalledAsync(
221239
222240 // It is safely filled up here instead
223241 var bytesRead = UnmarshalledRuntime . InvokeUnmarshalled < BufferParams , int > (
224- $ " FileReaderComponent.FillBufferUnmarshalled" ,
242+ FileReaderComponent . FillBufferUnmarshalled ,
225243 new BufferParams
226244 {
227245 TaskId = id ,
@@ -260,10 +278,10 @@ public static void EndReadFileUnmarshalledAsyncError(long taskId, string error)
260278 taskCompletionSource . SetException ( new BrowserFileReaderException ( error ) ) ;
261279 }
262280
263- #if NET5
281+ #if NET5_0_OR_GREATER
264282 internal async Task < IJSObjectReference > GetJSObjectReferenceAsync ( ElementReference elementRef , int fileIndex )
265283 {
266- return await CurrentJSRuntime . InvokeAsync < IJSObjectReference > ( " FileReaderComponent.GetJSObjectReference" , elementRef , fileIndex ) ;
284+ return await CurrentJSRuntime . InvokeAsync < IJSObjectReference > ( FileReaderComponent . GetJSObjectReference , elementRef , fileIndex ) ;
267285 }
268286#endif
269287
@@ -300,7 +318,7 @@ private async Task InitializeAsync()
300318
301319 private async Task < bool > IsLoaded ( )
302320 {
303- return await CurrentJSRuntime . InvokeAsync < bool > ( "window.hasOwnProperty" , " FileReaderComponent" ) ;
321+ return await CurrentJSRuntime . InvokeAsync < bool > ( "window.hasOwnProperty" , FileReaderComponent . InstanceName ) ;
304322 }
305323
306324 private async Task < T > ExecuteRawScriptAsync < T > ( string scriptContent )
@@ -311,4 +329,24 @@ private async Task<T> ExecuteRawScriptAsync<T>(string scriptContent)
311329 return await CurrentJSRuntime . InvokeAsync < T > ( "eval" , bootStrapScript ) ;
312330 }
313331 }
332+
333+ internal static class FileReaderComponent
334+ {
335+ public static readonly string InstanceName = nameof ( FileReaderComponent ) ;
336+
337+ public static readonly string ClearValue = $ "{ InstanceName } .{ nameof ( ClearValue ) } ";
338+ public static readonly string Dispose = $ "{ InstanceName } .{ nameof ( Dispose ) } ";
339+ public static readonly string FillBufferUnmarshalled = $ "{ InstanceName } .{ nameof ( FillBufferUnmarshalled ) } ";
340+ public static readonly string GetFileCount = $ "{ InstanceName } .{ nameof ( GetFileCount ) } ";
341+ public static readonly string GetFileInfoFromElement = $ "{ InstanceName } .{ nameof ( GetFileInfoFromElement ) } ";
342+ public static readonly string GetJSObjectReference = $ "{ InstanceName } .{ nameof ( GetJSObjectReference ) } ";
343+ public static readonly string OpenRead = $ "{ InstanceName } .{ nameof ( OpenRead ) } ";
344+ public static readonly string ReadFileMarshalledAsync = $ "{ InstanceName } .{ nameof ( ReadFileMarshalledAsync ) } ";
345+ public static readonly string ReadFileSliceAsync = $ "{ InstanceName } .{ nameof ( ReadFileSliceAsync ) } ";
346+ public static readonly string ReadFileUnmarshalledAsync = $ "{ InstanceName } .{ nameof ( ReadFileUnmarshalledAsync ) } ";
347+ public static readonly string RegisterDropEvents = $ "{ InstanceName } .{ nameof ( RegisterDropEvents ) } ";
348+ public static readonly string RegisterPasteEvent = $ "{ InstanceName } .{ nameof ( RegisterPasteEvent ) } ";
349+ public static readonly string UnregisterDropEvents = $ "{ InstanceName } .{ nameof ( UnregisterDropEvents ) } ";
350+ public static readonly string UnregisterPasteEvent = $ "{ InstanceName } .{ nameof ( UnregisterPasteEvent ) } ";
351+ }
314352}
0 commit comments