title: VB.NET stream read calculate crc32
body: <p>I am reading files using stream reader and writer with a buffer. Is it possible to calculate the crc32 checksum of the file at the same time so that it would evaluate the buffer during each iteration, and the result would be the crc32 of the complete file, instead of having to read teh file twice? Here is the code:</p>
  
  <pre><code>   Public Function APPEND_FILE_TO_FILE(ByVal SOURCE_FILE As String, ByVal DESTINATION_FILE As String)
          Dim nn As New FileInfo(SOURCE_FILE)
          Dim BUFFER(BUFFER_SIZE) As Byte
          Dim BYTES_READ As Long = 0
          Using inFile As New System.IO.FileStream(SOURCE_FILE, IO.FileMode.Open, IO.FileAccess.Read)
              Using outFile As New System.IO.FileStream(DESTINATION_FILE, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
                  Do
                      BYTES_READ = inFile.Read(BUFFER, 0, BUFFER_SIZE)
                      TOTAL_PROCESSED_DATA += BYTES_READ ' GUI related
                      outFile.Write(BUFFER, 0, BYTES_READ)
                  Loop While BYTES_READ &gt; 0
              End Using
          End Using
      End Function
  </code></pre>
  
  <p>CRC32 function:</p>
  
  <pre><code>Public Function GetCRC32(ByVal sFileName As String) As String
      Try
          Dim FS As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
          Dim CRC32Result As Integer = &amp;HFFFFFFFF
          Dim Buffer(4096) As Byte
          Dim ReadSize As Integer = 4096
          Dim Count As Integer = FS.Read(Buffer, 0, ReadSize)
          Dim CRC32Table(256) As Integer
          Dim DWPolynomial As Integer = &amp;HEDB88320
          Dim DWCRC As Integer
          Dim i As Integer, j As Integer, n As Integer
          'Create CRC32 Table
          For i = 0 To 255
              DWCRC = i
              For j = 8 To 1 Step -1
                  If (DWCRC And 1) Then
                      DWCRC = ((DWCRC And &amp;HFFFFFFFE) \ 2&amp;) And &amp;H7FFFFFFF
                      DWCRC = DWCRC Xor DWPolynomial
                  Else
                      DWCRC = ((DWCRC And &amp;HFFFFFFFE) \ 2&amp;) And &amp;H7FFFFFFF
                  End If
              Next j
              CRC32Table(i) = DWCRC
          Next i
          'Calcualting CRC32 Hash
          Do While (Count &gt; 0)
              For i = 0 To Count - 1
                  n = (CRC32Result And &amp;HFF) Xor Buffer(i)
                  CRC32Result = ((CRC32Result And &amp;HFFFFFF00) \ &amp;H100) And &amp;HFFFFFF
                  CRC32Result = CRC32Result Xor CRC32Table(n)
              Next i
              Count = FS.Read(Buffer, 0, ReadSize)
          Loop
          Return Hex(Not (CRC32Result))
      Catch ex As Exception
          Return ""
      End Try
  End Function
  </code></pre>