@@ -151,14 +151,6 @@ public async ValueTask CopyToAsync(Stream stream, int bufferSize = 65535, Cancel
151151
152152 using var encoder = new BrotliEncoder ( quality , window ) ;
153153
154- foreach ( var item in bufferWriter )
155- {
156- if ( item . Length > bufferSize )
157- {
158- bufferSize = item . Length ;
159- }
160- }
161-
162154 var buffer = ArrayPool < byte > . Shared . Rent ( bufferSize ) ;
163155 try
164156 {
@@ -182,14 +174,18 @@ public async ValueTask CopyToAsync(Stream stream, int bufferSize = 65535, Cancel
182174 }
183175
184176 // call BrotliEncoderOperation.Finish
185- var finalStatus = encoder . Compress ( ReadOnlySpan < byte > . Empty , buffer , out var consumed , out var written , isFinalBlock : true ) ;
186- if ( finalStatus != OperationStatus . Done )
177+ var finalStatus = OperationStatus . DestinationTooSmall ;
178+ while ( finalStatus == OperationStatus . DestinationTooSmall )
187179 {
188- MemoryPackSerializationException . ThrowCompressionFailed ( finalStatus ) ;
180+ finalStatus = encoder . Compress ( ReadOnlySpan < byte > . Empty , buffer , out var consumed , out var written , isFinalBlock : true ) ;
181+ if ( written > 0 )
182+ {
183+ await stream . WriteAsync ( buffer . AsMemory ( 0 , written ) , cancellationToken ) . ConfigureAwait ( false ) ;
184+ }
189185 }
190- if ( written > 0 )
186+ if ( finalStatus != OperationStatus . Done )
191187 {
192- await stream . WriteAsync ( buffer . AsMemory ( 0 , written ) , cancellationToken ) . ConfigureAwait ( false ) ;
188+ MemoryPackSerializationException . ThrowCompressionFailed ( finalStatus ) ;
193189 }
194190 }
195191 finally
0 commit comments