BufferedReader readLine() blocks

前端 未结 7 2123
轻奢々
轻奢々 2020-12-06 02:02

When receiving data using readLine(), even though I put a \"\\n\" at the end of the message using the .flush when sending the message, the while loop that reads my message s

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 02:40

    I tried a lot of solutions but the only one I found which wasn't blocking execution was:

    BufferedReader inStream = new BufferedReader(new 
    InputStreamReader(yourInputStream));
    String line;
    while(inStream.ready() && (line = inStream.readLine()) != null) {
        System.out.println(line);
    }
    

    The inStream.ready() returns false if the next readLine() call will block the execution.

提交回复
热议问题