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 并中断其他代码逐字节蚕食流并从中解释数据)?
请您参考如下方法:
这取决于您所说的可靠是什么意思。缓冲类是专门为尽可能防止 I/O 而定制的(因为这是缓冲区的全部要点),因此它们只保证它将执行最多 1 次缓冲区读取。返回的数据量完全取决于缓冲区中已有的数据量。
如果您需要精确数量的数据,则需要更改底层结构。特别是,您可能需要使用更大的缓冲区重新打开流。
如果这不是一个选项,您可以提供一个包装类,以便您可以拦截您需要的读取并将数据透明地提供给其他实际想要使用数据的代码。
