流操作

时间:2014-05-01 14:34:47   收藏:0   阅读:285
mamicode.com,码迷
 #region -- 流操作

        /// <summary>
        /// 复制流
        /// </summary>
        public static MemoryStream CopyStream(Stream stream)
        {
            MemoryStream mstream = new MemoryStream();
            CopyStream(stream, mstream);
            return mstream;
        }

        /// <summary>
        /// 复制流
        /// </summary>
        public static void CopyStream(Stream source, Stream target)
        {
            byte[] bytes = new byte[1024];//临时字节数据数组
            if (source.CanRead)
            {
                while (true)
                {
                    int length = source.Read(bytes, 0, bytes.Length);
                    target.Write(bytes, 0, length);
                    if (length < bytes.Length)
                    {
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Stream转字节数组
        /// </summary>
        /// <returns></returns>
        public static byte[] ReadStream(Stream stream)
        {
            using (MemoryStream mstream = new MemoryStream())
            {
                CopyStream(stream, mstream);
                return mstream.ToArray();
            }
        }
        #endregion
mamicode.com,码迷

 

流操作,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!