Applying a regular expression to a Java I/O Stream

2019-01-23 17:54发布

问题:

I seek an example of applying a regular expression to a Java I/O stream that doesn't simply convert the stream to a string as I would like to preserve binary data. Most of the examples on the Internet focus on text data...

回答1:

The needed functionality is not present on Java Standard. You will have to use jakarta regexp, and specifically, the StreamCharacterIterator class. This class encapsulates a InputStream for use in regexp operations.

If you want to use the standard regular expression package, I would suggest take a the source from the previous class here and change the contract by implementing CharSequence instead of CharacterIterator.



回答2:

Convert the stream to a byte array.



回答3:

Try to use Ragel - regular expression tool with transitions callbacks.

Can applied to streams and chunks.



回答4:

Regex operations must be performed on strings, which are encoded bytes of binary data. You can't perform regex operations on bytes of data you have no idea what they represent.