Solve the problem that bufferedReader. readLine of is blocked at the end of reading

  • 2021-11-01 03:35:38
  • OfStack

bufferedReader. readLine () read to last block

Recently, I am doing an imageserver. After simplifying the requirements, I use socket to respond to HTTP requests, so as to intercept the required data stream and write it to the file on the server side, thus completing the client to upload pictures to the server.

Because we only want to intercept one part of the data stream from the client. This prevents us from reading and writing to the file as we often do, and uses bufferedInputStream. read () when the stream has been read to the end > 0 or inputStream. read () > 0 as a criterion for the end of an while statement cannot return-1 in the data stream obtained using socket (because the client is an form form submitted through the browser, it cannot tell the server that the socket data has been sent. Therefore, the read () method is still waiting for the client to send a message, resulting in a blockage).

But if we don't use bufferedInputStream. read (), we can't get the client-side data stream. So how do we get the data stream and avoid blocking when reading the data?

I solved it this way:


 int newread = 0;
 int totalread = 0;
 int contentLength = Integer.parseInt(headers.get("content-length"));
 byte[] bytes = new byte[contentLength];
 while (totalread < contentLength) {
        newread = bufferedInputStream.read(bytes, totalread, contentLength - totalread);
        totalread += newread;
 }

headers: The custom map object is used to store the values of Content-Length in the previously parsed http request message

Reading data cyclically in this way can solve two problems:

1. Avoid blocking the whole program by using read () method.

2. When the read data stream is large, it can also prevent the read () method from being completely read.

In this way, the problem can be solved, perfect!

readline () of BufferedReader stepping on the pit

Just stepped on a small pit of readLine () method

There is no problem with the original function, but according to the following code, my lineStr1 is straight for Null. It seems that there are many related problems on the Internet, but my problems are not the same;

Here is the code in question:


BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //  Read the result 
            System.out.println(" Get :"+br.readLine());// Note that this is printed first 
            String lineStr = br.readLine();// And then in the obtained 

Later, I explained the readLine method according to others, and found that it was because the readLine method read 1 line, and it would read the next 1 line without closing, and I only need to read 1 line, so I took out the original print data. When assigning value to lineStr, it printed the next 1 line without anything at all, so I deleted the above print to solve this problem

Here are the changes:


BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //  Read the result 
            String lineStr = br.readLine();// Direct assignment 

Related articles: