Click here to Skip to main content
15,868,420 members
Articles / Mobile Apps
Article

CrcStream stream checksum calculator

Rate me:
Please Sign up or sign in to vote.
4.66/5 (23 votes)
8 Oct 2005CPOL1 min read 124.8K   2K   43   23
Make better use of time by calculating CRCs on-the-fly.

Introduction

CRC (Cyclic Redundancy Check) is commonly used as a way to confirm that a file had not corrupted during download. While convenient, it takes some time to read the data off of the disk after downloading for the check. It would be convenient if applications checked the CRC on-the-fly during download, so as not to waste idle CPU time and disk read time.

Downloading is done at a relatively leisurely pace (typically anywhere between 5-300kb/s) and over a long period of time, so it makes for a great opportunity to process data without impeding performance. Although ugly and impractical for most applications (it'd be safe to assume that most users think they've "broken the intarweb" when they see a hex number), displaying the CRC to the user immediately as a download finishes can often be a well-appreciated bonus.

This class passively calculates CRCs as data passes through it, ready to be used at any time.

Using the code

To calculate the CRC of a file as it is read to the end, create a new CrcStream passing the FileStream as an argument, and use the ReadCrc property to retrieve the CRC. Be sure to use the new CrcStream instead of the file stream to read from the file; otherwise the checksum will not be calculated.

C#
//Open a file stream, encapsulate it in CrcStream
FileStream file = new FileStream(filename, FileMode.Open);
CrcStream stream = new CrcStream(file);

//Use the file somehow -- in this case, read it as a string
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();

//Print the checksum
Console.WriteLine("CRC: " + stream.ReadCrc.ToString("X8"));

There are four public members in addition to the abstract Stream overrides:

  • ReadCrc - gets the checksum of the data that was read through the stream.
  • WriteCrc - gets the checksum of the data that was written to the stream.
  • ResetChecksum - resets the CRC values.
  • Stream - gets the encapsulated stream.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Canada Canada
The cows are here to take me home now...

Comments and Discussions

 
GeneralExcellent! Pin
JonKristian31-Mar-11 22:56
JonKristian31-Mar-11 22:56 
GeneralMy vote of 5 Pin
drago697-Mar-11 20:59
drago697-Mar-11 20:59 
GeneralMy vote of 5 Pin
ChewsHumans6-Sep-10 12:00
ChewsHumans6-Sep-10 12:00 
GeneralRe: My vote of 5 Pin
ChewsHumans6-Sep-10 13:11
ChewsHumans6-Sep-10 13:11 
NewsCrcStream in VB.net Pin
Mike19537-Nov-08 11:16
Mike19537-Nov-08 11:16 
QuestionHow does it handle big files? Pin
rgiejef5-Mar-08 2:27
rgiejef5-Mar-08 2:27 
AnswerRe: How does it handle big files? Pin
Rei Miyasaka5-Mar-08 2:35
Rei Miyasaka5-Mar-08 2:35 
GeneralRe: How does it handle big files? Pin
rgiejef13-Mar-08 1:31
rgiejef13-Mar-08 1:31 
GeneralRe: How does it handle big files? Pin
Rei Miyasaka13-Mar-08 10:05
Rei Miyasaka13-Mar-08 10:05 
GeneralRe: How does it handle big files? Pin
Christian Loft16-Mar-08 21:55
Christian Loft16-Mar-08 21:55 
GeneralRe: How does it handle big files? Pin
Buzz Weetman22-Apr-08 5:01
Buzz Weetman22-Apr-08 5:01 
GeneralRe: How does it handle big files? Pin
Rei Miyasaka22-Apr-08 9:31
Rei Miyasaka22-Apr-08 9:31 
GeneralYet another thank you Pin
zimmerware24-May-07 10:49
zimmerware24-May-07 10:49 
GeneralSmall Enhancement Pin
nerd_biker20-Mar-07 2:37
nerd_biker20-Mar-07 2:37 
GeneralRe: Small Enhancement Pin
Rei Miyasaka20-Mar-07 11:12
Rei Miyasaka20-Mar-07 11:12 
QuestionAdapted Version Pin
FernandoNunes30-Mar-06 2:45
FernandoNunes30-Mar-06 2:45 
AnswerRe: Adapted Version Pin
Rei Miyasaka31-Mar-06 6:49
Rei Miyasaka31-Mar-06 6:49 
Cool Big Grin | :-D

The code works fine as far as I can tell. I just gave it a quick run through reading a file. Definitely a lot more convenient if you know you're going to be dealing with a file rather than some other type of stream.

A couple reasons it won't affect performance:
1. When you override a member of a class, internally it does the same thing as when you call two methods one after another and pass parameters along -- just that it does that bookkeeping and method calling for you automatically. Even if it does make a slight difference, it'd only be a couple dozen CPU cycles per call at most. Each call takes probably at least a few million cycles, so the difference is immeasurable.
2. On an even broader scale, the time it takes to load a file off the disk generally dwarfs the time it takes to calculate the CRC (modern hard drives are that slow). Performance is a bit more significant only after the first time you read a file, because Windows caches the file onto memory.

Thanks!
GeneralFantastic! Pin
Emma Burrows15-Mar-06 23:57
Emma Burrows15-Mar-06 23:57 
GeneralRe: Fantastic! Pin
Rei Miyasaka16-Mar-06 0:00
Rei Miyasaka16-Mar-06 0:00 
Questionchecksum? Pin
Christoph Ruegg8-Oct-05 13:39
Christoph Ruegg8-Oct-05 13:39 
AnswerYep, checksum Pin
Rei Miyasaka8-Oct-05 14:11
Rei Miyasaka8-Oct-05 14:11 
GeneralRe: Yep, checksum Pin
Christoph Ruegg8-Oct-05 15:01
Christoph Ruegg8-Oct-05 15:01 
GeneralRe: Yep, checksum Pin
Rei Miyasaka9-Oct-05 21:02
Rei Miyasaka9-Oct-05 21:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.