tags:

views:

45

answers:

1

On one of my RedHat Linux Servers a couple devices have been setup. /dev/ap and /dev/reuter. They are AP and Reuters news feeds. At the unix command line I can do "cat /dev/ap" and it waits until a message comes down the feed and prints it out to stdout. As soon as there is a pause in the stream cat completes. I tried "more" and got the same results, same with less -f (complete msg, could be luck) and tail -f had no output in an hour.

I know these are streams, but when I try to open a Java BufferReader on new Reader("/dev/ap") I get no output. Using the following run method:

public void run() {
    String line = null;
    while(true) {
        try {
            while((line = bsr.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}  

Couple questions:
1. Are some of the unix commands limited to opening streams from files? e.g. tail? 2. What Am I doing wrong on the Java side that I can't capture the output? Wrong stream type, wrong wrapper type? Jim

+2  A: 

Your approach seems sound, but readLine() is very particular about what constitutes a line of text. You might look at the raw stream:

cat /dev/ap | hexdump -C

You could also try read(), as seen in this fragment that reads from /dev/zero:

BufferedReader in = new BufferedReader(new FileReader("/dev/zero"));
for (int i = 0; i < 10; i++) {
    System.out.print(in.read() + " ");
}
System.out.println();
in.close();

Addendum: For serial I/O, consider RXTX or similar libraries.

trashgod
Apparently, need to treat this dev as a serial port which requires javacomm.
Jim Jones
@Jim Jones: It's never easy. :-) I've added some links above.
trashgod
Yah in fact, I think I'll have to use RXTX because it is a virtual serial port.
Jim Jones