Skip to content

Commit 4b78311

Browse files
authored
TIKA-4683 -- charset detector dep mgmt and order in AutoDetectReader (#2800)
1 parent d90734b commit 4b78311

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

tika-core/src/main/java/org/apache/tika/detect/AutoDetectReader.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ public class AutoDetectReader extends BufferedReader {
4747
private static final EncodingDetector DEFAULT_DETECTOR;
4848

4949
static {
50-
DEFAULT_DETECTOR = new CompositeEncodingDetector(
51-
DEFAULT_LOADER.loadServiceProviders(EncodingDetector.class));
50+
// Use DefaultEncodingDetector so SPI-discovered detectors are run in the
51+
// pinned order (HtmlEncodingDetector, UniversalEncodingDetector, Icu4jEncodingDetector,
52+
// then anything else by class name). Otherwise the order would be whatever
53+
// ServiceLoader yields from classpath/jar order, which is fragile.
54+
DEFAULT_DETECTOR = new DefaultEncodingDetector(DEFAULT_LOADER);
5255
}
5356

5457
private final Charset charset;
@@ -79,8 +82,7 @@ private AutoDetectReader(TikaInputStream tis, Metadata metadata,
7982

8083
public AutoDetectReader(InputStream stream, Metadata metadata, ServiceLoader loader)
8184
throws IOException, TikaException {
82-
this(getTikaInputStream(stream), metadata,
83-
new CompositeEncodingDetector(loader.loadServiceProviders(EncodingDetector.class)));
85+
this(getTikaInputStream(stream), metadata, new DefaultEncodingDetector(loader));
8486
}
8587

8688
public AutoDetectReader(InputStream stream, Metadata metadata)

tika-parsers/tika-parsers-standard/tika-parsers-standard-package/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@
6868
<artifactId>tika-parser-digest-commons</artifactId>
6969
<version>${project.version}</version>
7070
</dependency>
71+
<dependency>
72+
<groupId>${project.groupId}</groupId>
73+
<artifactId>tika-encoding-detector-html</artifactId>
74+
<version>${project.version}</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>${project.groupId}</groupId>
78+
<artifactId>tika-encoding-detector-icu4j</artifactId>
79+
<version>${project.version}</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>${project.groupId}</groupId>
83+
<artifactId>tika-encoding-detector-universal</artifactId>
84+
<version>${project.version}</version>
85+
</dependency>
7186
<dependency>
7287
<groupId>${project.groupId}</groupId>
7388
<artifactId>tika-parser-font-module</artifactId>

0 commit comments

Comments
 (0)