Csharp/C Sharp/File Stream/Stream Null — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:45, 26 мая 2010

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];
            }
        }
    }
}