none
Am I reading the file by chunks correctly? I'm seeing differing results. RRS feed

  • Question

  • [Byte[]] $all = New-Object Byte[]($FileStream.Length)
    [Byte[]] $Buffer = New-Object Byte[](4096)
    [int]$BytesRead = 0
                           
    $FileStream.Seek(0, [System.IO.SeekOrigin]::Begin) | Out-Null
    
    while (($BytesRead = $FileStream.Read($Buffer, 0, 4096)) -gt 0) {
       $all += $buffer              
    }
    
    $all2 = [System.IO.File]::ReadAllBytes($file)

    $FileStream is a FileStream object opened to $file with Open and Read.   The $all.count ends up being significantly larger than $all2 so my question is, how is my loop to chunk the filestream incorrect?  The concatentation to the array is just a test, eventually different things will be done insisde of the loop in place of the $all +=, but I wanted to make sure I was doing it correctly before moving on.

    Thanks for any assistance.

    Tuesday, April 8, 2014 1:11 PM

Answers

  • $buffer is always 4096. $out+=$buffer will always add Number of reads times 4086 which is not the length of the file.

    Try this:
    $flength=0
    while(($BytesRead = $FileStream.Read($Buffer, 0, 4096)) -gt 0){$flength+=$BytesRead}
    Write-Host "Total bytes read $flength" -fore green


    ¯\_(ツ)_/¯

    • Marked as answer by thepip3r Tuesday, April 8, 2014 4:40 PM
    Tuesday, April 8, 2014 4:17 PM

All replies

  • $buffer is always 4096. $out+=$buffer will always add Number of reads times 4086 which is not the length of the file.

    Try this:
    $flength=0
    while(($BytesRead = $FileStream.Read($Buffer, 0, 4096)) -gt 0){$flength+=$BytesRead}
    Write-Host "Total bytes read $flength" -fore green


    ¯\_(ツ)_/¯

    • Marked as answer by thepip3r Tuesday, April 8, 2014 4:40 PM
    Tuesday, April 8, 2014 4:17 PM
  • Thanks jrv... that actually makes perfect sense because I was computing hashes on the outputs and getting the same values but the "Lengths" of the arrays were different so I was trying to wrap my head around that.  But I guess if it's a lot of blank information, it doesn't matter.
    Tuesday, April 8, 2014 4:38 PM