BufferedReader.readLine() 如何处理 EOF 或慢速输入?

How does BufferedReader.readLine() handle EOF or slow input?

文档说:

readLine() Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

这是否意味着,当输入中只有 "hello" 之类的东西时,readLine() 会等到 \n 字符出现,或者它是否能够识别 EOF 或什么?

在输入出现 "Hello" 并且 5 秒后 " world!" 的情况下,函数 return 会是什么?

答案在文档中,尽管有些含蓄 -

public String readLine() throws IOException; Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

我认为这暗示你它可以通过给你 null 作为 return 值来识别 EOF。

这是另一个答案:

How to see if a Reader is at EOF?

Does that mean, that when there is something like "hello" on the input and nothing more, the readLine() will wait until the \n character comes, or is it capable of recognizin EOF or something?

它将等到收到行终止符或流结束符。如果接收到流的结尾,它将 return 部分行,并且下一次 null

What will the function return in situation where on the input appears "Hello" and after 5 seconds " world!"?

  1. 如果您正在从套接字读取并且设置的读取超时时间短于五秒,它将抛出 SocketTimeoutException
  2. 否则如果 EOL 或 EOS 与“world!”一起出现或者如果设置了读取超时,它将 return "Hello world!".
  3. 否则会阻塞