Skip to content

Commit

Permalink
Respect the actualy bytes number read from file stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
blueww committed Mar 4, 2024
1 parent 4232afb commit 3d50ea5
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Storage/Storage/File/Cmdlet/SetAzureStorageFileContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,27 @@ await DataMovementTransferHelper.DoTransfer(() =>
using (FileStream stream = File.OpenRead(localFile.FullName))
{
byte[] buffer = null;
long lastBlockSize = 0;
for (long offset = 0; offset < fileSize; offset += blockSize)
for (long offset = 0; offset < fileSize;)
{
long currentBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset;
long targetBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset;

// create new buffer, the old buffer will be GC
buffer = new byte[currentBlockSize];
lastBlockSize = currentBlockSize;
buffer = new byte[targetBlockSize];

await stream.ReadAsync(buffer: buffer, offset: 0, count: (int)currentBlockSize);
int actualBlockSize = await stream.ReadAsync(buffer: buffer, offset: 0, count: (int)targetBlockSize);
if (!fipsEnabled && hash != null)
{
hash.AppendData(buffer);
hash.AppendData(buffer, 0, actualBlockSize);
}

Task task = UploadFileRangAsync(fileClient,
new HttpRange(offset, currentBlockSize),
new MemoryStream(buffer),
new HttpRange(offset, actualBlockSize),
new MemoryStream(buffer, 0, actualBlockSize),
progressHandler);
runningTasks.Add(task);

offset += actualBlockSize;

// Check if any of upload range tasks are still busy
if (runningTasks.Count >= maxWorkers)
{
Expand Down

0 comments on commit 3d50ea5

Please sign in to comment.