有人可以帮我简单的算法吗?

发布于 2024-08-27 15:08:49 字数 215 浏览 8 评论 0原文

我想解析http请求下载的所有字节如果它们> 100kb,如果不是,将它们连接到缓冲区,并且当缓冲区达到>时100kb 再次解析它们并将 uffer 清空。但不是解析所有文件数据并在最终块中获取 ex: 2kb。它必须检查下一个块,以及是否最终将其连接起来(例如:final_chunk.Length + 2kb),

重点是我每次都需要解析至少 100kb 块,不少于。

I want to parse all bytes downloaded by http request if they are > 100kb, if they are not, to concat them to a buffer and when the buffer gets > 100kb to parse them again and null the uffer. But not to parse all the file data and in the final chunk to get ex: 2kb. It must check the next chunk and if it's final to concat it (ex: final_chunk.Length + 2kb)

The point is that I need to parse at least 100kb chunks every time, nothing less.

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

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

发布评论

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

评论(3

往事风中埋 2024-09-03 15:08:49

正如 PoweRoy 所说,你已经有了算法,你只需要编码它......让我一步一步地讲。

  1. 声明本地缓冲区对象
  2. 发出异步/同步 HTTP 请求(我更喜欢异步调用)
  3. 将其保存到缓冲区
  4. 检查缓冲区大小(如果大于 100kb)
  5. 如果缓冲区大小不大于 100kb 将数据附加到缓冲区
  6. (如果大于 100kb) 读取缓冲并清除它。
  7. 转到步骤 2

As PoweRoy Said you already got algorithm you just need code it... Let me put it step by step.

  1. Declare local buffer object
  2. Make Async/Synchronous HTTP request (I prefer Async Call)
  3. Save it to buffer
  4. Check buffer size (if it is > 100kb)
  5. If buffer size not >100kb Append the Data to buffer
  6. if it >100kb read the buffer and clear it.
  7. Goto step 2
惯饮孤独 2024-09-03 15:08:49

我想这样就可以了。我只需要用字节数组重写它。你发现任何错误吗?

        class SmartChunk {

        private int ChunkSize;
        private int DataSize;
        private int pos;
        private int len;
        private string buff = "";

        public SmartChunk(int InitChunkSize, int DataLen) {
            pos = 0;
            len = DataLen;
            ChunkSize = InitChunkSize;
        }

        public string Append(string s) {
            if (pos + ChunkSize * 2 > len) ChunkSize = len - pos;

            if (s.Length >= ChunkSize) {
                return s;
            }
            else {
                buff += s;
                if (buff.Length >= ChunkSize) {
                    pos += buff.Length;
                    string b = buff;
                    buff = "";
                    return b;
                }
            }

            return null;
        }
    }

I guess that will do the trick. I just have to rewrite it with byte arrays. Do you find any bugs?

        class SmartChunk {

        private int ChunkSize;
        private int DataSize;
        private int pos;
        private int len;
        private string buff = "";

        public SmartChunk(int InitChunkSize, int DataLen) {
            pos = 0;
            len = DataLen;
            ChunkSize = InitChunkSize;
        }

        public string Append(string s) {
            if (pos + ChunkSize * 2 > len) ChunkSize = len - pos;

            if (s.Length >= ChunkSize) {
                return s;
            }
            else {
                buff += s;
                if (buff.Length >= ChunkSize) {
                    pos += buff.Length;
                    string b = buff;
                    buff = "";
                    return b;
                }
            }

            return null;
        }
    }
夜雨飘雪 2024-09-03 15:08:49

这是字节数组列表:

class SmartChunk {
        private int ChunkSize;
        private int DataSize;
        private int pos;
        private int len;
        private List<byte[]> buff;

        public SmartChunk(int InitChunkSize, int DataLen) {
            buff = new List<byte[]>();
            pos = 0;
            len = DataLen;
            ChunkSize = InitChunkSize;
        }

        public List<byte[]> Append(byte[] b) {
            if (pos + ChunkSize * 2 > len) ChunkSize = len - pos;

            if (b.Length >= ChunkSize) {
                List<byte[]> priv = new List<byte[]>();
                priv.Add(b);
                return priv;
            }
            else {
                buff.Add(b);
                int total_size = 0;
                foreach(byte[] inner in buff){
                    total_size += inner.Length; 
                }

                if (total_size >= ChunkSize) {
                    pos += total_size;

                    List<byte[]> temp = new List<byte[]>(buff);
                    //foreach (byte[] tmp in buff) temp.Add(tmp);
                    buff.Clear();
                    return temp;
                }
            }

            return null;
        }
    }

And here it is with list of byte arrays:

class SmartChunk {
        private int ChunkSize;
        private int DataSize;
        private int pos;
        private int len;
        private List<byte[]> buff;

        public SmartChunk(int InitChunkSize, int DataLen) {
            buff = new List<byte[]>();
            pos = 0;
            len = DataLen;
            ChunkSize = InitChunkSize;
        }

        public List<byte[]> Append(byte[] b) {
            if (pos + ChunkSize * 2 > len) ChunkSize = len - pos;

            if (b.Length >= ChunkSize) {
                List<byte[]> priv = new List<byte[]>();
                priv.Add(b);
                return priv;
            }
            else {
                buff.Add(b);
                int total_size = 0;
                foreach(byte[] inner in buff){
                    total_size += inner.Length; 
                }

                if (total_size >= ChunkSize) {
                    pos += total_size;

                    List<byte[]> temp = new List<byte[]>(buff);
                    //foreach (byte[] tmp in buff) temp.Add(tmp);
                    buff.Clear();
                    return temp;
                }
            }

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