gsingers opened a new pull request, #2972: URL: https://github.com/apache/tika/pull/2972
Backport of #2968 (`65a2f3ab6`) from `main` to `branch_3x`. JIRA: https://issues.apache.org/jira/browse/TIKA-4796 ## What `MagicDetector` recompiled its `Pattern` on every regex `detect()` call, even though both inputs to the compile — the pattern bytes and the case-insensitivity flag — are fixed at construction time. This compiles it once in the constructor and reuses it. `Pattern` is immutable and a new `Matcher` is still created per call, so the regex path remains safe for the shared, long-lived detector instances that `MimeTypes` hands out. ## Not a cherry-pick 3.x compiles the `Pattern` inline in `detect()`; `main` had already extracted the matching logic into `matchesBuffer()` behind `matches(byte[])`. The change is therefore hand-ported to the 3.x structure rather than cherry-picked. The same applies to the tests. The `main` PR added `testMatchesByteArrayRegEx` to cover the regex branch of `matches(byte[])`, which does not exist on 3.x — and 3.x already exercises the regex `detect()` path through `testDetectRegExPDF`, `testDetectRegExGreedy` and `testDetectRegExOptions`. Only the two guards that apply here are carried over, rewritten against `detect(InputStream, Metadata)`: - `testRegExDetectorRepeatedCallsStable` — repeated calls on one instance stay independent - `testRegExDetectorConcurrent` — 8 threads x 200 iterations against a shared detector ## Behavior change A malformed regex now raises `PatternSyntaxException` from the constructor rather than from the first `detect()`. Via `MagicMatch` this is still lazy, since `getDetector()` builds on first `eval()`, so loading `tika-mimetypes.xml` is unaffected. Documented with `@throws` on the constructor. ## Testing `./mvnw clean install -pl :tika-core` — 416 tests, 0 failures; `MagicDetectorTest` 13/13. Checkstyle and Spotless clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
