@@ -239,29 +239,27 @@ await DataMovementTransferHelper.DoTransfer(() =>
239
239
using ( FileStream stream = File . OpenRead ( localFile . FullName ) )
240
240
{
241
241
byte [ ] buffer = null ;
242
- long lastBlockSize = 0 ;
243
- for ( long offset = 0 ; offset < fileSize ; offset += blockSize )
242
+ for ( long offset = 0 ; offset < fileSize ; )
244
243
{
245
- long currentBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset ;
244
+ long targetBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset ;
246
245
247
- // Only need to create new buffer when chunk size change
248
- if ( currentBlockSize != lastBlockSize )
249
- {
250
- buffer = new byte [ currentBlockSize ] ;
251
- lastBlockSize = currentBlockSize ;
252
- }
253
- await stream . ReadAsync ( buffer : buffer , offset : 0 , count : ( int ) currentBlockSize ) ;
246
+ // create new buffer, the old buffer will be GC
247
+ buffer = new byte [ targetBlockSize ] ;
248
+
249
+ int actualBlockSize = await stream . ReadAsync ( buffer : buffer , offset : 0 , count : ( int ) targetBlockSize ) ;
254
250
if ( ! fipsEnabled && hash != null )
255
251
{
256
- hash . AppendData ( buffer ) ;
252
+ hash . AppendData ( buffer , 0 , actualBlockSize ) ;
257
253
}
258
254
259
255
Task task = UploadFileRangAsync ( fileClient ,
260
- new HttpRange ( offset , currentBlockSize ) ,
261
- new MemoryStream ( buffer ) ,
256
+ new HttpRange ( offset , actualBlockSize ) ,
257
+ new MemoryStream ( buffer , 0 , actualBlockSize ) ,
262
258
progressHandler ) ;
263
259
runningTasks . Add ( task ) ;
264
260
261
+ offset += actualBlockSize ;
262
+
265
263
// Check if any of upload range tasks are still busy
266
264
if ( runningTasks . Count >= maxWorkers )
267
265
{
0 commit comments