繁体   English   中英

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

[英]Reading http request from socket with null check java

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

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

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

像那样:

 while ((inputLine = in.readLine()) != null) {

但我假设你不想要一个空字符串,使用apache commons:

while(StringUtils.isNotBlank(inputLine = in.readLine())) {

编辑:

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

("").equals(in.readLine()) 

有点难以理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM