@@ -263,7 +263,7 @@ static void Error_EndOfStream() =>
263263 /// A <see cref="ValueTask"/> representing the asynchronous operation.
264264 /// </returns>
265265 public static ValueTask WriteAllTextAsync ( this VirtualFile file , string contents , CancellationToken cancellationToken = default ) =>
266- WriteAllTextAsync ( file , contents . AsMemory ( ) , Utf8NoBom , cancellationToken ) ;
266+ WriteAllTextAsync ( file , contents . AsMemory ( ) , encoding : null , cancellationToken ) ;
267267
268268 /// <summary>
269269 /// Asynchronously writes the specified string to the current file. If the file already exists, it is truncated and overwritten.
@@ -302,38 +302,9 @@ public static ValueTask WriteAllTextAsync(this VirtualFile file, ReadOnlyMemory<
302302 /// </returns>
303303 public static async ValueTask WriteAllTextAsync ( this VirtualFile file , ReadOnlyMemory < char > contents , Encoding ? encoding , CancellationToken cancellationToken = default )
304304 {
305- const int ChunkSize = 8192 ;
306-
307- if ( contents . IsEmpty )
308- return ;
309-
310- encoding ??= Utf8NoBom ;
311305 var stream = await file . OpenWriteAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
312-
313- var preamble = encoding . GetPreamble ( ) ;
314- if ( preamble . Length != 0 )
315- stream . Write ( preamble . AsSpan ( ) ) ;
316-
317- var bytes = ArrayPool < byte > . Shared . Rent (
318- encoding . GetMaxCharCount ( Math . Min ( ChunkSize , contents . Length ) ) ) ;
319-
320- try
321- {
322- var encoder = encoding . GetEncoder ( ) ;
323- while ( contents . Length != 0 )
324- {
325- var data = contents [ ..Math . Min ( ChunkSize , contents . Length ) ] ;
326- contents = contents [ data . Length ..] ;
327-
328- var encoded = encoder . GetBytes ( data . Span , bytes . AsSpan ( ) , flush : contents . IsEmpty ) ;
329- await stream . WriteAsync ( bytes . AsMemory ( 0 , encoded ) , cancellationToken ) . ConfigureAwait ( false ) ;
330- }
331- }
332- finally
333- {
334- await stream . DisposeAsync ( ) . ConfigureAwait ( false ) ;
335- ArrayPool < byte > . Shared . Return ( bytes ) ;
336- }
306+ await using var writer = new StreamWriter ( stream , encoding ! ) ;
307+ await writer . WriteAsync ( contents , cancellationToken ) . ConfigureAwait ( false ) ;
337308 }
338309
339310 /// <summary>
0 commit comments