Dee*_*101 3 .net c# networkstream stream
这与C#有关.我们有一种情况需要将整个源流复制到目标流中,除了最后16个字节.
编辑:流可以达到40GB,所以不能做一些静态byte []分配(例如:.ToArray())
看看MSDN文档,似乎只有当返回值为0时我们才能可靠地确定流的结束.返回值介于0和之间the requested size可能意味着字节"当前不可用"(这究竟意味着什么?)
目前,它按如下方式复制每个字节.inStream并且outStream是通用的 - 可以是内存,磁盘或网络流(实际上也是一些).
public static void StreamCopy(Stream inStream, Stream outStream)
{
    var buffer = new byte[8*1024];
    var last16Bytes = new byte[16];
    int bytesRead;
    while ((bytesRead = inStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        outStream.Write(buffer, 0, bytesRead);
    }
    // Issues:
    // 1. We already wrote the last 16 bytes into 
    //    outStream (possibly over the n/w)
    // 2. last16Bytes = ? (inStream may not necessarily support rewinding)
}
什么是可靠的方法来确保复制除了最后16个之外的所有内容?我能想到的使用Position和Length对进入数据流,但有一个疑难杂症MSDN,说
如果从Stream派生的类不支持搜索,则调用Length,SetLength,Position和Seek会抛出NotSupportedException..
Read between 1 and n bytes from the input stream.1
Append the bytes to a circular buffer.2
Write the first max(0, b - 16) bytes from the circular buffer to the output stream, where b is the number of bytes in the circular buffer.
Remove the bytes that you just have written from the circular buffer.
Go to step 1.
1This is what the Read method does – if you call int n = Read(buffer, 0, 500); it will read between 1 and 500 bytes into buffer and return the number of bytes read. If Read returns 0, you have reached the end of the stream.
2For maximum performance, you can read the bytes directly from the input stream into the circular buffer. This is a bit tricky, because you have to deal with the wraparound within the array underlying the buffer.
| 归档时间: | 
 | 
| 查看次数: | 627 次 | 
| 最近记录: |