如何使BufferedReader的peek更可靠?

2024-05-13 01:54:03 发布

您现在位置:Python中文网/ 问答频道 /正文

io.BufferedReader中有一个方便的peek函数。但是

peek([n])
    Return 1 (or n if specified) bytes from a buffer without advancing
the position. Only a single read on the raw stream is done to satisfy
the call. The number of bytes returned may be less than requested since
at most all the buffer’s bytes from the current position to the end are
returned.

它返回的字节太少。在

我在哪里可以得到可靠的多字节peek(不使用read并中断其他代码逐字节地蚕食流并从中解释数据)呢?在


Tags: ortheto函数fromioreadreturn
1条回答
网友
1楼 · 发布于 2024-05-13 01:54:03

这取决于你所说的“可靠”是什么意思。缓冲类是专门为防止I/O而定制的(因为这是缓冲区的全部点),因此它们只保证最多对缓冲区进行1读取。返回的数据量完全取决于缓冲区中已有的数据量。在

如果需要精确的数据量,则需要更改底层结构。尤其是,您可能需要使用更大的缓冲区重新打开流。在

如果这不是一个选项,那么您可以提供一个包装类,这样您就可以截获所需的读取,并将数据透明地提供给实际上想要使用数据的其他代码。在

相关问题 更多 >