Demonstrate the use of the Null stream as a bit bucket : Stream Null « File Stream « C# / C Sharp





Demonstrate the use of the Null stream as a bit bucket

Demonstrate the use of the Null stream as a bit bucket
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// NullStrm.cs -- demonstrate the use of the Null stream as a bit bucket.
//
//                Compile this program with the following command line:
//                    C:>csc NullStrm.cs
//
using System;
using System.IO;
namespace nsStreams
{
    public class NullStrm
    {
        static public void Main ()
        {
            byte [] b = new byte [256];
            int count = Stream.Null.Read (b, 0, b.Length);
            Console.WriteLine ("Read {0} bytes", count);
            string str = "Hello, World!";
            StringToByte (out b, str);
            Stream.Null.Write (b, 0, b.Length);

            // Attach a reader and writer to the Null stream
            StreamWriter writer = new StreamWriter (Stream.Null);
            StreamReader reader = new StreamReader (Stream.Null);
            writer.Write ("This is going nowhere");
            str = reader.ReadToEnd ();
            Console.Write ("The string read contains {0} bytes", str.Length);
        }

        // Convert a buffer of type string to byte
        static void StringToByte (out byte [] b, string str)
        {
            b = new byte [str.Length];
            for (int x = 0; x < str.Length; ++x)
            {
                b[x] = (byte) str [x];
            }
        }
    }
}



           
       










Related examples in the same category