Why does ReadRequest use bufio.Reader?

huangapple go评论83阅读模式
英文:

Why does ReadRequest use bufio.Reader?

问题

方法http.ReadRequest接受bufio.Reader而不是io.Reader作为参数。这背后的原因是什么?

英文:

The method http.ReadRequest takes a bufio.Reader instead of an io.Reader. What is the reason behind this?

答案1

得分: 0

它无论如何都会使用缓冲读取器(buffered reader)。但是,它不会自动将您的io.Reader包装到缓冲区中,因为bufio.Reader的特性是可以从底层的io.Reader中获取更多数据,而算法实际上可能不需要那么多数据。而且,由于底层的io.Reader无法将多余的数据推回,这些数据将保留在缓冲区中。如果自动包装,那么在请求被读取后,缓冲区将丢失,并且您的io.Reader将变得损坏。因此,它要求您明确地包装您的读取器以防止数据丢失。

英文:

It will use buffered reader anyway. But it will not automatically wrap your io.Reader into buffer because of bufio.Reader nature - it can grab more data from underlying io.Reader that algorithm actually need. And, since underlying io.Reader cannot push back that exceed data, it will remain in that buffer. In case of autowrap that buffer would be lost after request was read and your io.Reader become corrupted. Thus, it ask you to explicitly wrap your reader to prevent data loss.

huangapple
  • 本文由 发表于 2014年2月13日 04:52:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/21739332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定