The ready and Readline operations of bufferedreader and the premature EOF exceptions

Source: Internet
Author: User

Some users use the bufferedreader class ready when reading data returned by the server:

1 While (reader. Ready () {2 // execute the read operation, that is, Readline 3}

This method is often used, but the returned results are empty. Why? I checked the help document and checked it online, and summarized it as follows:

1. ready is used to check whether the stream is ready to be read. It is a non-blocking method, so it will return immediately. Because the server is not ready to be read, it will return immediately, therefore, all read results are null.

2. readline is a blocking method. As long as the connection is not disconnected, it will wait until something is returned. When will it return NULL? It will only return null when it reads the end of the data stream.

From: http://blog.csdn.net/neusoftware_20063500/article/details/3723176

In fact, data delay and other problems often occur when reading network data. In this case, java. Io. ioexception: premature EOF is easily encountered when you directly read data in the network stream. You can use the ready function to check whether bufferedreader is ready.

While (! Reader. Ready) {// blocking, wait for a while} while (reader. Readline ()! = NULL) {// execute the operation}

When reader. Readline has been read, false is returned if you continue to perform the ready operation. Therefore, the following two sections of code may cause an endless loop:

1.

While (reader. Readline ()! = NULL) {// execute the operation while (! Reader. Ready) {// blocking, waiting for a while }}

2.

While (reader. Readline ()! = NULL) {// execute the operation} while (! Reader. Ready) {// blocking, waiting for a while}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.