Java BufferedReader action on character?
enter code here
I'm reading a stream from a device in Linux which contains hexidecimal letters and is delimited by "^M". Whenever the device is ready for more information it sends the character ">". The stream looks like this:
^M^M01 02 F3^M00 01 F3 3E^M>"
I need to detect the char > and perform an action on that char. Is there a way I can delimit on ">" and have t开发者_运维问答hat character read?
Basically it currently looks like this
01 02 F3
00 01 F3 3E
I need > to trigger a separate action. It does not trigger the read buffer if just a single char is in there for some reason. I'm delmiting on ^M. I need > to do something else.
It's quite possible to do this. I recently wrote a project which read input from multiple mice using a very similar interface. The binary data from the mice came in via file I/O.
I think buffered I/O is inappropriate here. Buffered I/O will try to fill a buffer for you, for performance reasons. But it seems that your app will be spending most of its time waiting for new characters. If you're not reading in bulk, then raw byte reads InputStream will do the trick: They will return immediately after the requested/expected number of bytes (1 in your case) have been received. In your case, after reading the >
character, you'd issue another single-byte read for the following char.
You'll need to figure out what to do with intervening characters. Once you've processed your >
and the following char, what do you want to do with any following characters that are not >
?
What I did in my app was to directly read single bytes from an InputStream
. This would block, so it had to be done in a separate Thread
that spent most of its time waiting. When the first character came in, I knew to read a handful more (just like you will), which I could then process. I had some kind of queue set up for passing this information to the rest of my app - you'll have to work out some kind of synchronization for your inter-thread communication.
EDIT: Multiple small edits to clarify points. Sorry if this ends up confusing.
精彩评论