java更改reader字体颜色,Java BufferedReader对字符的操作?

enter code hereI'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 that 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值