Resolving "IOException: Underlying input stream returned zero bytes" errors
Hi guys,
I'm currently working on a simple application that reads GPS receiver data from a serial port using the RXTX library. However, everytime I try to read the resulting InputStream (which I convert to a BufferedReader) using readLine(), after 2 or 3 lines of reading normally, I always get the following exception:
GPSNotifier::run(): I/O error encountered while reading from COM4
java.io.IOException: Underlying input stream returned zero bytes
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at discomm.gps.GPSNotifier.run(GPSNotifier.java:260)
at java.lang.Thread.run(Thread.java:619)
I'm not sure how to fix this problem, since it occurs everytime I run the application. The code for opening the stream is as follows:
// Open up the port: no problems here ...
CommPortIdentifier comm = CommPortIdentifier.getPortIdentifier(port_name);
CommPort port = comm.open(APP_NAME, OPEN_TIMEOUT);
SerialPort serial_port = (SerialPort) port;
serial_port.setSerialPortParams(PORT_BAUDRATE, PORT_DATABITS, PORT_STOPBITS, PORT_PARITY);
BufferedReader in = BufferedReader(new InputStreamReader(serial_port.getInputStream()));
String line =null;
// Problem is here ...
while ((line = in.readLine()) !=null){
...
}
The IOException always occurs when readLine() is called. Any ideas on how to fix this problem?
Thanks in advance,
Simon Liu

