栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在C#中将Stream转换为byte []?[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在C#中将Stream转换为byte []?[重复]

调用下一个函数,例如

byte[] m_Bytes = StreamHelper.ReadToEnd (mystream);

功能:

public static byte[] ReadToEnd(System.IO.Stream stream)    {        long originalPosition = 0;        if(stream.CanSeek)        {  originalPosition = stream.Position;  stream.Position = 0;        }        try        { byte[] readBuffer = new byte[4096]; int totalBytesRead = 0; int bytesRead; while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) {     totalBytesRead += bytesRead;     if (totalBytesRead == readBuffer.Length)     {         int nextByte = stream.ReadByte();         if (nextByte != -1)         {  byte[] temp = new byte[readBuffer.Length * 2];  Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);  Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);  readBuffer = temp;  totalBytesRead++;         }     } } byte[] buffer = readBuffer; if (readBuffer.Length != totalBytesRead) {     buffer = new byte[totalBytesRead];     Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); } return buffer;        }        finally        { if(stream.CanSeek) {      stream.Position = originalPosition;  }        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452079.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号