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

[2119 byte] By [simmo149a] at [2007-11-27 2:22:26]
# 1
I would use DataInputStream.read() here. There's no guarantee that the incoming data is text and no guarantee that it can be encoded into Unicode. So any form of Reader is out. I suspect there is no guarantee that it consists of lines either, so readLine() in any form is out too.
ejpa at 2007-7-12 2:26:35 > top of Java-index,Core,Core APIs...
# 2

Fair enough, but the data that the GPS receiver outputs is supposed to be completely in ASCII, and each GPS data set is delimited by a newline. Naturally I assumed that the use of BufferedReader/readLine() would be appropriate here. Doesn't matter though, I'll reimplement the reading part by just read()'ing from a raw InputStream.

simmo149a at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...
# 3
Reader and readLine() might be OK if the box is guaranteed to be always working and never delivering junk. I don't trust hardware that much myself.
ejpa at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...
# 4

Hi,

The problem is that, at the end of a line, the GPS receiver sends a line feed character (ascii code = 10) and/or a carriage return character (ascii code = 13). Then, you must test if the current character stocked in your "line" variable (while ((line = in.readLine()) != null) ) is 10 or 13. If YES, you must break your while function to avoid reading the rest of the line. It should solve your problem.

Alex

alexja at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...
# 5
> It should solve your problemHow? readLine() will remove both those characters. And they would have been at the end of the line so there is no 'rest of the line'.This doesn't make much sense.
ejpa at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...
# 6

I don't use readLine() but read()

A bit of code:

BufferedReader inStream = new BufferedReader(new InputStreamReader(inputStream));

byte ch = 0;

char myChar;

int charVal;

try{

while((ch = (byte)inStream.read()) != -1){

if((ch == 13) || (ch == 10)){ //In ASCII code: 13 = Carriage return, 10 = line feed. When the GPS receiver sends those characters, the while loop must be broken to avoid an IOException

break;

}else{

myChar = (char)ch;

charVal = myChar;

//System.out.print("byte=" + ch +" myChar=" + myChar + " charVal=" + charVal + "\n");

System.out.print(myChar);

}

}

alexja at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...
# 7
Hi,I have the same IOException problem. The suggested solution is not helping because the exception again happen at "while((ch = (byte)inStream.read()) != -1){ "Any other suggestions?
accca at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...
# 8

> The problem is that, at the end of a line, the GPS

> receiver sends a line feed character (ascii code =

> 10) and/or a carriage return character (ascii code =

> 13).

I am finding it hard to believe the 'and/or' part. Surely the thing is consistent about what it considers a line terminator to be? And I've never heard of a system that sent a \r without a \n, although the world is full of systems that send a \n without a preceding \r.

So if you get \r you should almost certainly read and discard a following \n.

ejpa at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...
# 9

Okay, I have EXACTLY the same problem, but I may have found the problem.

As of Java 1.4.someversion if the underlying buffer stream gets zero bytes it throws an exception with the handy little message "Underlying input stream returned zero bytes". Now while my little gadget is not a GPS, it also uses a bunch of ASCII characters.

Might the problem be if there is ever a pause in the stream (for example inbetween sample frames) the exception is thrown?

I'm hoping this bit of info will allow someone to fix the problem and post it here :)

Mercera at 2007-7-12 2:26:36 > top of Java-index,Core,Core APIs...