该死的代码
三聪的博客
 #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

 

posted on 2014-04-30 10:06  三聪  阅读(248)  评论(0编辑  收藏  举报

作者:gateluck
出处:http://gateluck.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。