ottlinger commented on code in PR #714:
URL: https://github.com/apache/creadur-rat/pull/714#discussion_r3852213520
##########
apache-rat-core/src/main/java/org/apache/rat/analysis/TikaProcessor.java:
##########
@@ -166,20 +177,30 @@ public static String process(final Document document)
throws RatDocumentAnalysis
* @throws UnsupportedCharsetException on unsupported charset.
*/
private static Charset detectCharset(final InputStream stream, final
DocumentName documentName) throws IOException, UnsupportedCharsetException {
- final int bytesForCharsetDetection = 256;
- CharsetDetector encodingDetector = new
CharsetDetector(bytesForCharsetDetection);
- encodingDetector.setText(stream);
- CharsetMatch charsetMatch = encodingDetector.detect();
- if (charsetMatch != null) {
- try {
- return Charset.forName(charsetMatch.getName());
- } catch (UnsupportedCharsetException e) {
- DefaultLog.getInstance().warn(String.format("Unsupported
character set '%s' in file '%s'",
- charsetMatch.getName(), documentName));
- throw e;
+ stream.mark(BYTES_FOR_CHARSET_DETECTION);
+ try {
+ byte[] sample = stream.readNBytes(BYTES_FOR_CHARSET_DETECTION);
+ if (sample.length == 0) {
+ DefaultLog.getInstance().debug(String.format("No contents in
file '%s'", documentName));
+ return null;
+ }
+
+ Metadata metadata = new Metadata();
+ ParseContext parseContext = new ParseContext();
+
+ try (TikaInputStream tis = TikaInputStream.get(sample, metadata)) {
+ List<EncodingResult> results = ENCODING_DETECTOR.detect(tis,
metadata, parseContext);
+
+ if (results.isEmpty()) {
+ DefaultLog.getInstance().debug(String.format("No encoding
found for file '%s'", documentName));
+ return null;
+ }
Review Comment:
We do not have an explicit test for unsuppoorted character sets. Tika
handles this internally and returns no charset. If no charset is returned RAT
will mark as UNKNOWN if I'm not too mistaken.
--
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]