从流净读取河段 [英] .Net Read Stream Segment from a Stream

查看:135
本文介绍了从流净读取河段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来读取数据流的一部分。我有一个40000字节流,需要只读从位置8000至15000,problably东西很容易,但我似乎无法找到一个简单的方法来获取流段。

I am trying to find a way to read part of a stream. I have a stream with 40000 bytes and need to read only from position 8000 to 15000, problably something easy but I can't seem to find an easy way to get a stream segment.

推荐答案

由于读取数据流的一部分,应该很容易,我假设你真正需要一个新的流对象只能访问给定的段底层流。

Since reading a part of the stream should be easy enough, I'm assuming you're actually in need of a new Stream-object that only accesses the given segment of the underlying stream.

IE浏览器。你想是这样的:

ie. you want something like this:

Stream segment = new StreamSegment(underlyingStream, 8000, 7000);

我有这样一个类,你可以在这里找到它:<一href="https://lassevk.kilnhg.com/$c$c/LVK-for-NET/net-40/stable/Files/LVK.IO/PartialStream.cs?rev=tip&nr="相对=nofollow> LVK.IO.PartialStream 。它依赖于其他类从我的类库,特别是<一个href="https://lassevk.kilnhg.com/$c$c/LVK-for-NET/net-40/stable/Files/LVK.IO/WrapperStream.cs?rev=tip&nr="相对=nofollow> LVK.IO.WrapperStream ,但你可以找到它都在那里,只要抓住你实际需要的一些文件(如果你决定使用它们。)

I have such a class, and you can find it here: LVK.IO.PartialStream. It relies on other classes from my class library, in particular LVK.IO.WrapperStream, but you can find it all there, just grab the few files you actually need (if you decide to use them.)

要使用它,你必须指定PartialStream对象是否拥有底层流。如果是这样,在废弃PartialStream对象,它也将处理底层流的。

To use it, you have to specify whether your PartialStream object owns the underlying stream. If it does, when you dispose of the PartialStream object, it will also dispose of the underlying stream.

因此​​,对于上面的例子:

So for the above example:

Stream segment = new PartialStream(underlyingStream, false, 8000, 7000);

假设它不应该拥有的基本流(或通过作为第二个参数。)

assuming it shouldn't own the underlying stream (or pass true as the second parameter.)

注:

  • 构造上述PartialStream对象将重新定位的基本流,以该段的开始(在上面的例子中位置8000。)
  • 您应该不是你正在使用的部分流对象,同时使用底层流,因为有一些内部簿记事情关系到段内的位置。如果重新底层流不通过部分流,该行为是不确定的。

这篇关于从流净读取河段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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