I am trying to find an efficient way to do a pattern match on a ByteArrayOutputStream whose size exceeds String's max size.
Doing a pattern match on a ByteArrayOutputStream that fits into a single String is trivial:
private boolean doesStreamContainPattern(Pattern pattern, ByteArrayOutputStream baos) throws IOException {
/*
* Append external source String to output stream...
*/
if (pattern != null) {
String out = new String(baos.toByteArray(), "UTF-8");
if (pattern.matcher(out).matches()) {
return true;
}
}
/*
* Some other processing if no pattern match
*/
return false;
}
But if the size of baos
exceeds String max size, the problem turns into:
- Feeding
baos
into multiple Strings. - "Sliding" the pattern matching over the concatenation of those multiple Strings (i.e. the original
baos
content).
Step 2 looks more challenging then Step 1 but I know that utilities like Unix sed do just that on a file.
What is the right way to accomplish that?