What 0 returned by InputStream.read() means? How to handle this?

后端 未结 4 774
不思量自难忘°
不思量自难忘° 2020-12-16 00:10

What 0 (number of bytes read) returned by InputStream.read means? How to handle this situation?

Update: I mean read(byte[] b)

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 00:57

    I observed the same behavior (reading 0 bytes) when I build a swing console output window and made a reader-thread for stdout and stderr via the following code:

                    this.pi = new PipedInputStream();
                    po = new PipedOutputStream((PipedInputStream)pi);
                    System.setOut(new PrintStream(po, true));
    When the 'main' swing application exits, and my console window is still open I read 0 from this.pi.read(). The read data was put on the console window resulting in a race condition some how, just ignoring the result and not updating the console window solved the issue.

提交回复
热议问题