Directly use c++ stream buffer to e.g. decompress

会有一股神秘感。 提交于 2020-06-01 06:45:05

问题


My use case is reading a compressed file from disk, de-compressing it incrementally and using the resulting data.

Currently I am reading file contents into a temporary buffer allocated by me, point the decompression API at this buffer, and so on. The question is about understanding whether the temporary buffer really is necessary or helpful in this case.

In an experiment, I opened a stream to a text file and called get() once. In the debugger I can see that the filebuffer in the stream already contains the following characters in the text stream, as expected. (On msvc I found it under std::ifstream::_Filebuffer::_IGfirst)

I am looking for a portable way to access this buffer I see in the debugger, feed the decompression API with it, and then continue reading the file.

I don't understand why I should copy the filebuffer to my buffer (with e.g. read()) in this particular case where I will promptly consume the buffer contents and move on. I'm not questioning the merits of buffer I/O in general.

EDIT: I did further experiments, and it seems that the internal stream buffer doesn't get used if the target of read() is itself sufficiently large. Apparently the case I was worried about, doesn't really come up.

来源:https://stackoverflow.com/questions/61321441/directly-use-c-stream-buffer-to-e-g-decompress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!