網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在拿到一個(gè) Stream 如何優(yōu)雅將這個(gè) Stream 保存到代碼
最優(yōu)雅的方法應(yīng)該是通過(guò) CopyTo 或 CopyToAsync 的方法
using (var fileStream = File.Create("C:\\lindexi\\File.txt")) { inputStream.Seek(0, SeekOrigin.Begin); iputStream.CopyTo(fileStream); }
這里的?inputStream.Seek(0, SeekOrigin.Begin);
?不一定需要,請(qǐng)根據(jù)你自己的需求,如你只需要將這個(gè) Stream 的從第10個(gè)byte開(kāi)始復(fù)制等就不能采用這句代碼
用異步方法會(huì)讓本次寫入的時(shí)間長(zhǎng)一點(diǎn),但是會(huì)讓總體性能更好,讓 CPU 能處理其他任務(wù)
using (var fileStream = File.Create("C:\\lindexi\\File.txt")) { await iputStream.CopyToAsync(fileStream); }
注意使用 CopyToAsync 記得加上 await 哦,執(zhí)行到這句代碼的時(shí)候,就將執(zhí)行交給了 IO 了,大部分的 IO 處理都不需要 CPU 進(jìn)行計(jì)算,這樣能達(dá)到總體性能更好
另外如果 iputStream 是外面?zhèn)魅氲模敲次也唤ㄗh在這個(gè)方法里面釋放,為什么呢?我用的好好的一個(gè)Stream傳入一個(gè)業(yè)務(wù)就被干掉了
其次的方法是自己控制內(nèi)存復(fù)制緩存,此方法將會(huì)多出一次內(nèi)存復(fù)制
public static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[1024]; int len; while ( (len = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, len); } } // 使用方法如下 using (Stream file = File.Create("C:\\lindexi\\File.txt")) { CopyStream(input, file); }
此方法的作用就是讓你修改?new byte[1024]
?的值,讓你可以控制復(fù)制的緩存
接下來(lái)就是一些不推薦的方法了,但是寫的時(shí)候方便
using (var stream = new MemoryStream()) { input.CopyTo(stream); File.WriteAllBytes(file, stream.ToArray()); }
上面這個(gè)方法將會(huì)復(fù)制兩次內(nèi)存,而且如果 input 這個(gè)資源長(zhǎng)度有 1G 就要占用 2G 的資源
和上面差不多的是申請(qǐng)一個(gè)大的緩存,如下面代碼:
public void SaveStreamToFile(string fileFullPath, Stream stream) { if (stream.Length == 0) return; using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length)) { byte[] bytesInStream = new byte[stream.Length]; stream.Read(bytesInStream, 0, (int)bytesInStream.Length); fileStream.Write(bytesInStream, 0, bytesInStream.Length); } }
從效率和代碼的優(yōu)雅其實(shí)都不如 CopyTo 方法,而且因?yàn)?stream.Length 作為長(zhǎng)度沒(méi)有決定緩存,所以也不如第二個(gè)方法
public void SaveStreamToFile(Stream stream, string filename) { using(Stream destination = File.Create(filename)) { Write(stream, destination); } } public void Write(Stream from, Stream to) { for(int a = from.ReadByte(); a != -1; a = from.ReadByte()) { to.WriteByte( (byte) a ); } }
原文鏈接:https://blog.csdn.net/Pei_hua100/article/details/126159850
相關(guān)推薦
- 2022-12-29 python查看包版本、更新單個(gè)包、卸載單個(gè)包的操作方法_python
- 2022-09-09 Python?cv.Canny()方法參數(shù)與使用方法_python
- 2022-06-08 Spring Cloud Openfeign分析
- 2022-05-20 golang?croncli?定時(shí)器命令詳解_Golang
- 2022-06-25 React服務(wù)端渲染和同構(gòu)的實(shí)現(xiàn)_React
- 2023-06-04 React中的合成事件是什么原理_React
- 2022-06-06 Python字符串常規(guī)操作小結(jié)_python
- 2022-09-13 c++如何實(shí)現(xiàn)歸并兩個(gè)有序鏈表_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支