某項(xiàng)目需要輸出一個(gè)數(shù)據(jù)文件,該文件由2部分組成,即文件頭信息和數(shù)據(jù)。
專業(yè)領(lǐng)域包括網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、商城系統(tǒng)網(wǎng)站開(kāi)發(fā)、微信營(yíng)銷、系統(tǒng)平臺(tái)開(kāi)發(fā), 與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開(kāi)發(fā)公司不同,成都創(chuàng)新互聯(lián)公司的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營(yíng)銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。
項(xiàng)目是使用C#語(yǔ)言在.NET Framework 4上創(chuàng)建的。
拿到這個(gè)需求,首先想到的是定義一個(gè)Writer類,在寫(xiě)入方法中創(chuàng)建一個(gè)文件流,使用BinaryWriter封裝,寫(xiě)入所需要的各種數(shù)據(jù)??雌饋?lái)就像這樣:
public void Write() { using (Stream stream = OpenFileStream()) using (BinaryWriter writer = new BinaryWriter(stream)) { WriteHeader(writer); WriteData(writer); } } private void WriteHeader(BinaryWriter writer) { // ...... } private void WriteData(BinaryWriter writer) { // ...... }
不過(guò)隨后需求就發(fā)生了變化,因?yàn)閿?shù)據(jù)敏感,需要加密。于是想到對(duì)文件頭部分使用BinaryWriter寫(xiě)入,而后面的數(shù)據(jù)部分,先使用CryptoStream包裝流,再使用BinaryWriter寫(xiě)入。于是改成這樣:
public void Write() { using (Stream stream = OpenFileStream()) { using (BinaryWriter headerWriter = new BinaryWriter(stream)) { WriteHeader(headerWriter); } using (CryptoStream cryptoStream = new CryptoStream(stream, GetCryptoTransform(), CryptoStreamMode.Write)) using (BinaryWriter dataWriter = new BinaryWriter(cryptoStream)) { WriteData(dataWriter); } } }
改過(guò)之后,問(wèn)題產(chǎn)生了——在使用headerWriter的using語(yǔ)句結(jié)束時(shí),會(huì)自動(dòng)調(diào)用headerWriter.Dispose(),而這個(gè)方法會(huì)調(diào)用BaseStream.Close(),也就是說(shuō),文件流被關(guān)閉了,那么后面嘗試寫(xiě)入數(shù)據(jù)時(shí)就會(huì)拋出異常。
雖然BinaryWriter有一個(gè)構(gòu)造方法可以申明不關(guān)閉流:
public BinaryWriter( Stream output, Encoding encoding, bool leaveOpen )
但這個(gè)構(gòu)造方法是在.NET 4.5才加入的,項(xiàng)目是用的4.0的Framework,所以必須另外想辦法。而且后面的CryptoStream也存在同樣的問(wèn)題,而它可沒(méi)有提供不關(guān)閉流的構(gòu)造。
這里可以想到兩個(gè)辦法來(lái)處理:
1. 在所有內(nèi)容都寫(xiě)完之后再統(tǒng)一Dispose各種操作對(duì)象和流對(duì)象。
2. 定義一個(gè)從Stream繼承的StreamWrapper,將Close和Dispose都重載并實(shí)現(xiàn)為空方法,再定義一個(gè)ReallyClose方法來(lái)真正關(guān)閉封裝的流。
使用第1種方法,就像這樣:
public void Write() { using (Stream s = OpenFileStream()) using (BinaryWriter headWriter = new BinaryWriter(s)) using (CryptoStream cs = new CryptoStream(s, GetCryptoTransform(), CryptoStreamMode.Write)) using (BinaryWriter dataWriter = new BinaryWriter(cs)) { WriterHeader(headWriter); WriteData(dataWriter); } }
而且如果一個(gè)文件分成了很多很多段的話,這個(gè)using列表就太長(zhǎng)了。但這不是問(wèn)題,問(wèn)題是如果WriteHeader中拋出異常,那么cs和dataWriter這兩個(gè)對(duì)象就浪費(fèi)了。所以,可以考慮使用try {...} finally {...} 來(lái)實(shí)現(xiàn)using,
public void Write() { Stream stream = null; BinaryWriter headWriter = null; CryptoStream cs = null; BinaryWriter dataWriter = null; try { stream = OpenFileStream(); headWriter = new BinaryWriter(stream); WriterHeader(headWriter); cs = new CryptoStream(stream, GetCryptoTransform(), CryptoStreamMode.Write); dataWriter = new BinaryWriter(cs); WriteData(dataWriter); } finally { if (dataWriter != null) { dataWriter.Dispose(); } if (cs != null) { cs.Dispose(); } if (headWriter != null) { headWriter.Dispose(); } if (stream != null) { stream.Dispose(); } } }
解決問(wèn)題,但代碼量大,而且容易出錯(cuò)。比如,要記得在WriterHeader里面Flush,這個(gè)原本會(huì)在Dispose()自動(dòng)執(zhí)行的操作(對(duì)于CryptoStream,需要執(zhí)行FlushFinalBlock())。
相對(duì)來(lái)說(shuō),寫(xiě)一個(gè)StreamWrapper靠譜多了。不過(guò)使用RealClose就失去了IDisposable的意義,所以稍稍改變一下,定義一個(gè)變量來(lái)允許Dispose時(shí)關(guān)閉流。
class StreamWrapper : Stream { private readonly Stream stream; public Stream Stream { get { return stream; } } public StreamWrapper(Stream stream) { this.stream = stream; IsLeavingOpen = true; } public override void Flush() { stream.Flush(); } public override long Seek(long offset, SeekOrigin origin) { return stream.Seek(offset, origin); } public override void SetLength(long value) { stream.SetLength(value); } public override int Read(byte[] buffer, int offset, int count) { return stream.Read(buffer, offset, count); } public override void Write(byte[] buffer, int offset, int count) { stream.Write(buffer, offset, count); } public override bool CanRead { get { return stream.CanRead; } } public override bool CanSeek { get { return stream.CanSeek; } } public override bool CanWrite { get { return stream.CanWrite; } } public override long Length { get { return stream.Length; } } public override long Position { get { return stream.Position; } set { stream.Position = value; } } public override void Close() { if (IsLeavingOpen) { return; } stream.Close(); base.Close(); } protected override void Dispose(bool disposing) { if (IsLeavingOpen) { return; } if (disposing) { stream.Dispose(); } base.Dispose(disposing); } public bool IsLeavingOpen { get; set; } }
如果稍稍改變一下寫(xiě)入數(shù)據(jù)的接口,使用起來(lái)也非常方便
public void Write() { using (Stream fileStream = OpenFileStream()) using (StreamWrapper stream = new StreamWrapper(fileStream)) { WriteHeader(stream); WriteData(stream); stream.IsLeavingOpen = false; } } private void WriteHeader(Stream stream) { using (BinaryWriter writer = new BinaryWriter(stream)) { // ...... } } private void WriteData(Stream stream) { using (CryptoStream cryptoStream = new CryptoStream(stream, GetCryptoTransform(), CryptoStreamMode.Write)) using (BinaryWriter writer = new BinaryWriter(cryptoStream)) { // ...... } }
本文名稱:分段輸出到同一個(gè)Stream(.NET)
文章源于:http://aaarwkj.com/article46/gjgjeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、云服務(wù)器、定制開(kāi)發(fā)、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、App開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)