在C#流重用 [英] Stream Reuse in C#

查看:113
本文介绍了在C#流重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩弄我认为是一个简单的想法。我希望能够从某处(网站,文件系统,FTP)在文件中读取,就可以执行某些操作(压缩,加密等),然后保存到某个地方(地方可能是一个文件系统,FTP或其他) 。这是一个基本的管道设计。我想这样做是在文件中读取,并把它放到一个MemoryStream,然后在MemoryStream的数据进行操作,然后保存数据在某处的MemoryStream。我想我可以使用相同的流要做到这一点,但碰上了几个问题:




  1. 每次我用的StreamWriter或StreamReader的我需要关闭它,这关闭流,这样我可以不使用它了。这似乎是必须有一些方法来解决这一点。

  2. 某些文件可能是大的,所以我会用尽内存,如果我尝试读取整个事情在一次。



  3. 我希望能够旋转起来各工序作为单独的线程和具有压缩步骤尽快有开始流上的数据,然后尽快压缩对我可以开始保存它(例如)流可用的一些压缩的数据。是这样的事情可能很容易用C#流?任何人有想法就如何做到这一点最好的吗?



    谢谢,



    迈克


    解决方案

    使用一个辅助方法来驱动流:

     的静态公共无效复制音频流(流源,流目标)
    {
    字节[]缓冲区=新的字节[8 * 1024];

    INT大小;

    {
    尺寸= source.Read(缓冲,0,8 * 1024);
    target.Write(缓冲液,0,大小);
    }当(大小大于0);
    }

    您可以很容易地结合任何你需要:

     使用使用(iFile的的FileStream =新的FileStream(...))
    (的FileStream OFILE =新的FileStream(...))
    使用(DeflateStream oZip =新DeflateStream(不过outFile,CompressionMode.Compress))
    复制音频流(iFile的,oZip);

    根据您实际尝试做的,你会连锁流不同。这也使用相对少的内存,因为一旦在内存中进行操作只有数据。


    I've been playing around with what I thought was a simple idea. I want to be able to read in a file from somewhere (website, filesystem, ftp), perform some operations on it (compress, encrypt, etc.) and then save it somewhere (somewhere may be a filesystem, ftp, or whatever). It's a basic pipeline design. What I would like to do is to read in the file and put it onto a MemoryStream, then perform the operations on the data in the MemoryStream, and then save that data in the MemoryStream somewhere. I was thinking I could use the same Stream to do this but run into a couple of problems:

    1. Everytime I use a StreamWriter or StreamReader I need to close it and that closes the stream so that I cannot use it anymore. That seems like there must be some way to get around that.
    2. Some of these files may be big and so I may run out of memory if I try to read the whole thing in at once.

    I was hoping to be able to spin up each of the steps as separate threads and have the compression step begin as soon as there is data on the stream, and then as soon as the compression has some compressed data available on the stream I could start saving it (for example). Is anything like this easily possible with the C# Streams? ANyone have thoughts as to how to accomplish this best?

    Thanks,

    Mike

    解决方案

    Using a helper method to drive the streaming:

    static public void StreamCopy(Stream source, Stream target)
    {
        byte[] buffer = new byte[8 * 1024];
    
        int size;
        do
        {
          size = source.Read(buffer, 0, 8 * 1024);
          target.Write(buffer, 0, size);
        } while (size > 0);
    }
    

    You can easily combine whatever you need:

    using (FileStream iFile = new FileStream(...))
    using (FileStream oFile = new FileStream(...))
    using (DeflateStream oZip = new DeflateStream(outFile, CompressionMode.Compress))
        StreamCopy(iFile, oZip);
    

    Depending on what you are actually trying to do, you'd chain the streams differently. This also uses relatively little memory, because only the data being operated upon is in memory.

    这篇关于在C#流重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆