11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
33
4+ using SixLabors . ImageSharp . IO ;
45using SixLabors . ImageSharp . PixelFormats ;
56using SixLabors . ImageSharp . Processing ;
67
@@ -209,7 +210,7 @@ T PerformActionAndResetPosition(Stream s, long position)
209210 }
210211
211212 Configuration configuration = options . Configuration ;
212- using MemoryStream memoryStream = new ( ) ;
213+ using ChunkedMemoryStream memoryStream = new ( configuration . MemoryAllocator ) ;
213214 stream . CopyTo ( memoryStream , configuration . StreamProcessingBufferSize ) ;
214215 memoryStream . Position = 0 ;
215216
@@ -265,6 +266,11 @@ Task<T> PerformActionAndResetPosition(Stream s, long position, CancellationToken
265266 return PerformActionAndResetPosition ( ms , ms . Position , cancellationToken ) ;
266267 }
267268
269+ if ( stream is ChunkedMemoryStream cms )
270+ {
271+ return PerformActionAndResetPosition ( cms , cms . Position , cancellationToken ) ;
272+ }
273+
268274 return CopyToMemoryStreamAndActionAsync ( options , stream , PerformActionAndResetPosition , cancellationToken ) ;
269275 }
270276
@@ -276,11 +282,9 @@ private static async Task<T> CopyToMemoryStreamAndActionAsync<T>(
276282 {
277283 long position = stream . CanSeek ? stream . Position : 0 ;
278284 Configuration configuration = options . Configuration ;
279-
280- await using MemoryStream memoryStream = new ( ) ;
285+ await using ChunkedMemoryStream memoryStream = new ( configuration . MemoryAllocator ) ;
281286 await stream . CopyToAsync ( memoryStream , configuration . StreamProcessingBufferSize , cancellationToken ) . ConfigureAwait ( false ) ;
282287 memoryStream . Position = 0 ;
283-
284288 return await action ( memoryStream , position , cancellationToken ) . ConfigureAwait ( false ) ;
285289 }
286290
0 commit comments