Thursday, 13 September 2007

Lesson 3: Compressing Streams

Compression - useful in real-world projects to save space or bandwidth. .NF supports 2 new stream classes that can compress data.
  • GZIP & DEFLATE - 2 mthds inside .NF. Use industry-std compression algorithms. No intellectual-property issues.
  • size limit - can compress upto 4GB of uncompressed data.
  • said comp. mthds exposed by .NF as GZipStream() & DeflateStream().
  • gzip or DEFLATE? - GZIP allows for headers for xtra info. Thus if internal use DefalteStream - slightly smaller but if distributing externally use GZIP.
  • GZipStream() - allows comp.

How to Compress Data with a Compression Stream

  • Compression streams don't write to a resource (e.g. a file, memory) - they write to other streams.
  • typical scenario - read an existing file & write to a new compressed version of the file.
  • GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress) - a compression stream wraps the stream that contains (or will contain) compressed data.

Wednesday, 12 September 2007

lesson 2: Using Common Ref Types II

Understanding Readers & Writers

  • TextReader/TextWriter - abstract classes fro mwhich StreamReader/Writer are derived.
  • StringReader/Writer - to read/write to/fro in-memory strings.
  • StringWriter uses a 'StringBuilder', so it is very efficient at creating ever larger strings.
  • BinaryReader/BinaryWriter - for handling binary data to/from streams (i.e. binary files).
  • long number = reader.ReadInt64(); byre[] bytes = reader.ReadBytes(4);...
  • MemoryStream class - func. to create in-memory streams.
  • When use MemoryStream? Do time-intensive work inthe MemoryStream and then open the dest. file, flush the data to it, and close the file quickly.
  • BufferedStream - to wrap streams to improve performance by buffering reads & writes thro the stream. Writing out data to a stream directly does not perform very well. BufferStream wraps another stream object, to allow for writes to happen to a buffer. Only when the buffer is flushed does the data get pushed into the underlying stream.

Monday, 3 September 2007

lesson 2: Using Common Ref Types.

Ref Types Vs Value Types

  • Ref variable - holds the address of data rather than the data itself.
  • Assigning one ref. var to another - doesn't copy the data - merely creates 2nd copy of ref to same data.
  • 2500 built-in ref types - everything NOT derived from System.ValueType

Strings/StringBuilder

  • Immutable - cannot change - System.String.
  • Avoid temporary strings - s = "a"; s+= "qwerty"; - 2 strings created, of which only the last will have a ref. 1st garbage collected. Avoid unnecessary garbage collection here by using String class's Concat(), Join() & Format() mthds. Or use StringBuilder class to create mutable strings.
  • StringBuilder class - can span multiple stmts.

Create/Sort Arrays

  • int[] ar = {3,1,2};
  • Sort using static mthd - Array.Sort(ar);

Streams (System.IO.Stream)

  • common type - means of reading from/writing to disk.
  • StreamReader/StreamWriter - simplest - enable reading from/writing to txt files.
  • sr.ReadToEnd();

Exceptions

  • Catch block - skipped if no error encountered in try block.
  • define own exceptions - derive from System.ApplicationException.
  • Filtering Exceptions - mutiple catch blocks - runtime will only execute the 1st Catch block with a matching exception. THUS ORDER CATCH BLOCKS FROM MOST-SPECIFIC TO LEAST SPECIFIC.
  • Finally{} - runs whether an exception occured or not.
  • All code in try/catch blocks.