继承流; Read() 中的缓冲

发布于 2024-07-13 21:39:30 字数 165 浏览 7 评论 0原文

我继承了 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 技术交流群。

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

发布评论

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

评论(2

把时间冻结 2024-07-20 21:39:31

内部源返回固定大小的缓冲区? 如果是这种情况,这并不完全是 BufferedStream 所做的 - 这只是减少了对物理流的调用次数。 您需要一个单独的机制来缓存 - 您填充和清空的 MemoryStream 将是一个合理的选择。 例如(完全未经测试):

MemoryStream localBuffer = new MemoryStream();
bool ReadNextChunk()
{
    // clear
    localBuffer.Position = 0;
    localBuffer.SetLength(0);
    // get data
    byte[] chunk = null; // TODO - read from source
    if(chunk == null || chunk.Length == 0) return false; // EOF
    localBuffer.Write(chunk, 0, chunk.Length);
    localBuffer.Position = 0;
    return true;
}
public override int Read(byte[] buffer, int offset, int count)
{
    int bytes;
    if ((bytes = localBuffer.Read(buffer, offset, count)) > 0) return bytes;
    if (!ReadNextChunk()) return 0;
    return localBuffer.Read(buffer, offset, count);
}

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):

MemoryStream localBuffer = new MemoryStream();
bool ReadNextChunk()
{
    // clear
    localBuffer.Position = 0;
    localBuffer.SetLength(0);
    // get data
    byte[] chunk = null; // TODO - read from source
    if(chunk == null || chunk.Length == 0) return false; // EOF
    localBuffer.Write(chunk, 0, chunk.Length);
    localBuffer.Position = 0;
    return true;
}
public override int Read(byte[] buffer, int offset, int count)
{
    int bytes;
    if ((bytes = localBuffer.Read(buffer, offset, count)) > 0) return bytes;
    if (!ReadNextChunk()) return 0;
    return localBuffer.Read(buffer, offset, count);
}
成熟的代价 2024-07-20 21:39:31

这是你的“10 份开胃菜”(不确定这是否适用于全球)。

byte[] myBuffer = new byte[fixedSize];
int myBufferPos = fixedSize;

public int Read(byte[] buffer, int offset, int count)
{
    int copiedCount = 0
    while (copiedCount < count)
    {
        if (myBufferPos >= fixedSize)
        {
            //Read new fixed buffer into myBuffer
            // use break on no more buffer.
            myBufferPos = 0;
        }

        int bytesToCopy = fixedSize - myBufferPos;
        if (bytesToCopy > count - copiedCount)
            byteToCopy = count - copiedCount;

        Array.Copy(myBuffer, myBufferPos, buffer, offset, byteToCopy);

        offset += bytesToCopy;
        myBufferPos += bytesToCopy;
        copiedCount += bytesToCopy;
    }

    return copiedCount;
}

未经测试,因此可能存在一些错误。 不清楚您的源流的长度是否是其固定大小的精确倍数。 如果不是,那么最终的部分缓冲区需要一些额外的逻辑。

基本原则是维护自己的固定大小的缓冲区,并跟踪到目前为止读取消耗的缓冲区中的位置

Here is your "starter for 10" (not sure if that translates globally).

byte[] myBuffer = new byte[fixedSize];
int myBufferPos = fixedSize;

public int Read(byte[] buffer, int offset, int count)
{
    int copiedCount = 0
    while (copiedCount < count)
    {
        if (myBufferPos >= fixedSize)
        {
            //Read new fixed buffer into myBuffer
            // use break on no more buffer.
            myBufferPos = 0;
        }

        int bytesToCopy = fixedSize - myBufferPos;
        if (bytesToCopy > count - copiedCount)
            byteToCopy = count - copiedCount;

        Array.Copy(myBuffer, myBufferPos, buffer, offset, byteToCopy);

        offset += bytesToCopy;
        myBufferPos += bytesToCopy;
        copiedCount += bytesToCopy;
    }

    return copiedCount;
}

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

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