Skip to main content

Posts

Showing posts with the label Binary Streams

Data Serialization in .Net

Namespace: System.Runtime.Serialization Serialization is the process of converting object so that they can be transmitted over the network, or stored in file for later use, or passing them to a web service or any other application. .Net WebServices, Session Object, .Net Remoting and clipboard rely on Serialization. Binary Serialization: It’s efficient but not portable solution. String data = “Hello”; FileStream fs = new FileStream(“FileStream.Data”, FileMode.Create); BinaryFormatter bf = new BinarFormatter (); Bf.Serilize(fs, data); //Wrting Objects Bf.Serilize(fs,System.DateTime.Now); Fs.close(); Deserializing Objects FileStream fs = new FileStream(“FileStream.Data”, FileMode.Open); BinaryFormatter bf = new BinarFormatter (); String Data = (String)bf.Deserialize(fs); DateTime vTime = (DateTime) bf.Derialize(fs); To serialize a class one must the Serializeable attribute to that class. To omit some attribute from deseriali...

Compressing Streams in .Net

Namespace: System.IO.Compression There are two types of compression streams, GZip and Deflate . GZip leaves a standard header information block that can be used by the tools like Gzip to decompress the information. Deflate does not leaves any header information thus it is only decompressed by the respective application only. Both streams can only compress 4GB of uncompressed data. FileStream source = new FileStream(@“C:\file.dat”); FileStream destination = new FileStream(@“C:\file.gzip”); GZipStream Gs = new GZipStream(destination, CompressionMode.Compress); int b = source.ReadByte(); While(b!=-1) {                 Gs.WriteByte((byte)b);                 b = source.ReadByte(); } And to decompress a compressed file : FileStream source = new FileStream(@“ C:\file.gzip ”);   //reversed ...

Reading and Writing Files with .Net

Understanding Streams : Sequential and random access streams. All stream classes inherit from Stream class , these classes include: FileStream (System.IO), MemoryStream (System.IO), CryptoStream(System.Security), NetworkStream(System.Net), GZipStream(System.Compression). File class is an important class used to open file for reading/writing, Exist, Delete, Move and etc… Create/open Methods of File will return FileStream Object. For Sequential reading and writing some methods also return StreamReader and StreamWriter and they encapsulate FileSteam object. Few simple operations are identical to FileInfo object. MemoryStream is a specialized class provided to manage data in memory stream to gain some optimization. How to read from a File : FileStream theFile = File.Open(@“C:\boot.ini”, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(thefile); Console.Write(sr.ReadToEnd()); Sr.Close(); theFile.Close(); OR StreamReader s...