Reading HTTP request from socket with java null check

I am reading an HTTP request from a socket input stream

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

It works, but findbugs gives the following error: Dereference of the result of readLine() without nullcheck . The request ends with "" not eof . So how can I check for a null value here?

+5
source share
1 answer

Like:

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

But I assume you don't need an empty string, use apache commons:

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

Edit:

Also +1 for sodium comments. However, in my opinion, this is:

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

unreadable.

+4
source

All Articles