分段输出到同一个Stream(.NET)-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

分段输出到同一个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))
    {
        // ......
    }
}

文章名称:分段输出到同一个Stream(.NET)
文章URL:http://kswsj.cn/article/gjgjeg.html

其他资讯