从 C# 中的文件字段流式传输

发布于 2024-12-19 13:49:06 字数 131 浏览 1 评论 0原文

我有一个以流(一个用于输入,另一个用于输出)作为参数的函数。它是加密功能,因此流可以非常大。但是流,该函数需要的不是整个文件,而是文件字段。那么,我如何将文件字段(它可能非常大,因此没有 MemoryStreams 等)放入流中?抱歉我的英语不好。

I have function with streams(one for input and another for output) as parametrs. It is cryptographic fucnction so streams can be very big. But stream, that function need is not a whole file it is file field.So, how I can put file field(it may be very big so no MemoryStreams etc.) in stream? Sorry for my bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虐人心 2024-12-26 13:49:06

如果您的意思是您需要提供一个属于文件(或任何其他 Stream)一部分的 Stream,那么您通常会使用以下两种方法之一:

  • 创建一个 SubStream :封装(装饰)的 Stream 类一个内部 Stream 并跟踪已读取的数据量,并且只允许读取“n”个字节(在构造函数中提供),并且一旦消耗就会从 Read 返回 0(同时记住 Max 请求的字节和剩余字节)阅读)
  • 编写您的代码,以便它在内部根据一定的上限跟踪数据,并且永远不会过度读取。

如果您需要将该 Stream 传递到预先存在的代码/库,则第一个很有用。

在这两种情况下,IMO 设置流的代码应该担心“之前”的数据,而上面的代码只担心不读取太多数据。

编辑:这是我之前写的: http://protobuf -net.googlecode.com/svn/branches/v1/protobuf-net/SubStream.cs

If you mean you meed to supply a Stream that is a part of a file (or any other Stream) then you typically would use one of two approaches:

  • create a SubStream : Stream class that encapsulates (decorates) an inner Stream and keeps track of how much data has been read, and only allows Read of "n" bytes (supplied in the constructor), and would return 0 from Read once consumed (also remembering to Max the requested bytes and remaining bytes during Read)
  • write your code such that it internally tracks data against some cap, and simply never over-reads

The first is useful if you need to pass that Stream to pre-existing code / libraries.

In both cases, IMO the code that sets up the stream should worry about the "before" data, with the above just worrying about not reading too much.

Edit: here's one I wrote earlier: http://protobuf-net.googlecode.com/svn/branches/v1/protobuf-net/SubStream.cs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文