使用null check java从socket读取http请求

use*_*602 5 java findbugs inputstream

我正在从套接字输入流中读取HTTP请求

StringBuilder request = new StringBuilder();
String inputLine;
while (!(inputLine = in.readLine()).equals("")) {
    request.append(inputLine + "\r\n");
}
Run Code Online (Sandbox Code Playgroud)

它工作但是findbugs给出了以下错误:Dereference of the result of readLine() without nullcheck.请求以""不结束eof.那么如何在这里检查空值呢?

小智 4

像那样:

 while ((inputLine = in.readLine()) != null) {
Run Code Online (Sandbox Code Playgroud)

但我假设您不想要空白字符串,请使用 apache commons:

while(StringUtils.isNotBlank(inputLine = in.readLine())) {
Run Code Online (Sandbox Code Playgroud)

编辑:

钠的评论也+1。但在我看来:

("").equals(in.readLine()) 
Run Code Online (Sandbox Code Playgroud)

有点难以阅读。