您好,登錄后才能下訂單哦!
某項目需要輸出一個數據文件,該文件由2部分組成,即文件頭信息和數據。
項目是使用C#語言在.NET Framework 4上創建的。
拿到這個需求,首先想到的是定義一個Writer類,在寫入方法中創建一個文件流,使用BinaryWriter封裝,寫入所需要的各種數據。看起來就像這樣:
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) { // ...... }
不過隨后需求就發生了變化,因為數據敏感,需要加密。于是想到對文件頭部分使用BinaryWriter寫入,而后面的數據部分,先使用CryptoStream包裝流,再使用BinaryWriter寫入。于是改成這樣:
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); } } }
改過之后,問題產生了——在使用headerWriter的using語句結束時,會自動調用headerWriter.Dispose(),而這個方法會調用BaseStream.Close(),也就是說,文件流被關閉了,那么后面嘗試寫入數據時就會拋出異常。
雖然BinaryWriter有一個構造方法可以申明不關閉流:
public BinaryWriter( Stream output, Encoding encoding, bool leaveOpen )
但這個構造方法是在.NET 4.5才加入的,項目是用的4.0的Framework,所以必須另外想辦法。而且后面的CryptoStream也存在同樣的問題,而它可沒有提供不關閉流的構造。
這里可以想到兩個辦法來處理:
1. 在所有內容都寫完之后再統一Dispose各種操作對象和流對象。
2. 定義一個從Stream繼承的StreamWrapper,將Close和Dispose都重載并實現為空方法,再定義一個ReallyClose方法來真正關閉封裝的流。
使用第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); } }
而且如果一個文件分成了很多很多段的話,這個using列表就太長了。但這不是問題,問題是如果WriteHeader中拋出異常,那么cs和dataWriter這兩個對象就浪費了。所以,可以考慮使用try {...} finally {...} 來實現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(); } } }
解決問題,但代碼量大,而且容易出錯。比如,要記得在WriterHeader里面Flush,這個原本會在Dispose()自動執行的操作(對于CryptoStream,需要執行FlushFinalBlock())。
相對來說,寫一個StreamWrapper靠譜多了。不過使用RealClose就失去了IDisposable的意義,所以稍稍改變一下,定義一個變量來允許Dispose時關閉流。
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; } }
如果稍稍改變一下寫入數據的接口,使用起來也非常方便
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)) { // ...... } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。