c# 使用 Read 读取数据块

class Program
    {
        static void Main(string[] args)
        {

            Stream s = new MemoryStream();
            for (int i = 0; i < 500; i++)
            {
                s.WriteByte((byte)i);
            }
            s.Position = 0;

            // Now read s into a byte buffer with a little padding.
            byte[] bytes = new byte[s.Length + 10];
            int numBytesToRead = (int)s.Length;
            int numBytesRead = 0;
            do
            {
                // Read may return anything from 0 to 10.
                int n = s.Read(bytes, numBytesRead, 10);
                numBytesRead += n;
                numBytesToRead -= n;
            } while (numBytesToRead > 0);
            s.Close();

            Console.WriteLine("number of bytes read: {0:d}", numBytesRead);
        }
    }

 

posted @ 2021-03-26 14:26  摩诘  阅读(975)  评论(0编辑  收藏  举报