如何从 zlib 流读取任意字节?
我想一次将 zlib 流的 inflate()
的输出读取到缓冲区中,以便我可以解析出单行。
有没有办法指定 inflate()
返回的字节数,以便我可以监视换行符?
I would like to read the output of a zlib stream's inflate()
into a buffer one byte at a time, so that I can parse out single lines.
Is there a way to specify the number of bytes that inflate()
returns, so that I can watch for newlines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会带来糟糕的性能,因为
inflate()
函数可能会产生一些启动成本。我建议使用合理的输出缓冲区大小,然后在每次调用
inflate()
后迭代缓冲区,收集找到的所有完整行。当然,很可能会有一条“尾巴”,即一条不完整的线,您需要在下一轮中跟踪它。That would probably give horrible performance, since the
inflate()
function might have some start up costs.I would suggest just going with a reasonable output buffer size, and then iterating through the buffer after each call to
inflate()
, collecting all complete lines found. Of course there will very likely be a "tail", i.e. an incomplete line, that you will need to keep track of for the next round.当您调用
inflate()
时,您将传递一个指向z_stream_s
结构的指针。该结构体的z_stream_s::avail_out
成员变量正是用于指定输出缓冲区的大小。When you call
inflate()
you pass a pointer to az_stream_s
structure.z_stream_s::avail_out
member variable of that structure is exactly for specifying the size of the output buffer.