1

I tried to ask this question earlier, but I was unclear in my question. Java BufferedReader action on character?

Here is my problem.. I have a BufferedReader set to read from a device. It is reading well. I have it set to

if (Status.reader.ready()) {
    Lines = Status.reader.readLine();
}
if (Lines.contains(">")) {
    log.level1("ready to send data")
}

Buffered reader does not report the > until I've sent more data to the device. The problem is that when reader contains > it is not reporting ready. It holds onto the > until I input more data.

I tried the following and it returns nothing. It does not even return the log.level0()

Lines = ""

try {
    Lines = Status.reader.readLine();
} catch (IOException e) {
    Log.level0("Attempted to read blank line");
}

Here is the actual data sent:

^M^M01 02 F3^M00 01 F3 3E^M>

But BufferedReader ignores the > until more data has been sent then get a result like this:

>0102

When I check the actual data from the device from the command prompt, it returns what I'd expect, the > is present.

BufferedReader will not give me the >. Is there some way I can check for this char otherwise?

Community
  • 1
  • 1
Adam Outler
  • 1,651
  • 4
  • 19
  • 23

3 Answers3

5

The BufferedReader.readLine() method reads data a line at a time. That is, it will attempt to read characters until it sees an end-of-line sequence (e.g. "\n", "\r" or "\r\n") or the end of stream.

If your input data is not line oriented, then you should not be using readLine() to read it. I suggest that you do your own record / message extraction; e.g.

BufferedReader br = ...
StringBuilder sb = new StringBuilder(...);
int ch = br.read();
while (ch != -1 && ch != '>') {
    sb.append((char) ch);
    ch = br.read();
}
String record = sb.toString();
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • That is a very long procedure which adds objects. the final code ended up going from what it was to a while loop with a (char)lines.read(); – Adam Outler Jul 17 '10 at 02:28
  • @Adam Outler - I don't understand what you are saying, or how it is relevant to my answer. – Stephen C Jul 17 '10 at 03:27
1

Check this: http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/io/BufferedReader.html

I recommend that you use the function public int read() instead.

At google you can find a lot of examples1

oagostinho
  • 64
  • 4
0

With those F3s in there it looks to me like your data isn't even character-oriented let alone line-oriented. Is your device really Unicode-compliant?

I would use a BufferedInputStream.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • It transfers hex data with ^M delimiters. Then it Requests data with >. It's integer data. – Adam Outler Jul 14 '10 at 02:11
  • Exactly, so you shouldn't be using a Reader at all. See above. – user207421 Jul 15 '10 at 02:49
  • no, it works fine. I just needed to use read instead of readline. it is character oriented. It is very much char oriented. Passing data through (char) read(); worked very well and changed my delmiters to \r instead of raw ^M – Adam Outler Jul 17 '10 at 02:26
  • hexidecimal is a way of displaying chars. – Adam Outler Jul 17 '10 at 02:29
  • This answer misses the point. A device does not need to be Unicode compliant to produce data that you can read as characters in Java. Simply use an InputStreamReader providing an explicit `encoding` argument to specify the stream's character encoding. – Stephen C Jul 17 '10 at 03:32