分段输出到同一个Stream(.NET)-创新互联

某项目需要输出一个数据文件,该文件由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))
    {
        // ......
    }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:分段输出到同一个Stream(.NET)-创新互联
当前链接:http://myzitong.com/article/icgdg.html