Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading http request from socket with null check java

I'm reading HTTP request from socket input stream

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

It's working but findbugs gives the following bug: Dereference of the result of readLine() without nullcheck. Request ends with "" not eof. So how can I check null value here?

like image 371
user2950602 Avatar asked Sep 27 '22 22:09

user2950602


1 Answers

Like that:

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

But I assume that you don't want a blank string, use apache commons:

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

Edit:

Also +1 for sodium's comment. However in my opinion this:

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

is a bit unreadable.

like image 72
Tomek Avatar answered Oct 05 '22 22:10

Tomek