继承流; Read() 中的缓冲
我继承了 Stream 类,在其中我不知道如何正确实现 Read() 函数,因此我不会最终得到大量嵌套的 if 和难以调试的代码。 要点是从该流的源读取返回恒定大小的缓冲区(例如不可更改),但 Read() 函数接受不同的缓冲区大小。 我想添加 BufferedStream,但我认为这是个坏主意。 感谢帮助!
I have inherited Stream class, in which I don't know how to implement Read() function properly, so I won't end up with a lot of nested ifs and hard to debug code. The point is that reading from source of this stream returns constant sized buffer (e.g. not changeable), but Read() function accepts different buffer sizes. I though of adding BufferedStream, but I think that's bad idea. Thanks for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内部源返回固定大小的缓冲区? 如果是这种情况,这并不完全是 BufferedStream 所做的 - 这只是减少了对物理流的调用次数。 您需要一个单独的机制来缓存 - 您填充和清空的 MemoryStream 将是一个合理的选择。 例如(完全未经测试):
The inner source returns fixed-size buffers? If that case, that isn't quite what
BufferedStream
does - that simply reduces the number of calls to a physical stream. You'd need a separate mechanism to cache - a MemoryStream that you fill and empty would be a reasonable choice. For example (completely untested):这是你的“10 份开胃菜”(不确定这是否适用于全球)。
未经测试,因此可能存在一些错误。 不清楚您的源流的长度是否是其固定大小的精确倍数。 如果不是,那么最终的部分缓冲区需要一些额外的逻辑。
基本原则是维护自己的固定大小的缓冲区,并跟踪到目前为止读取消耗的缓冲区中的位置
Here is your "starter for 10" (not sure if that translates globally).
Untested so may have some bugs. Its unclear if your source stream has a length of exact multiples of its fixed size. If not then the final partial buffer needs a little extra logic.
The basic principle is to maintain your own buffer of the fixed size and track the position in that buffer so far consumed by reads