Search in sources :

Example 1 with Klv

use of org.codice.ddf.libs.klv.data.Klv in project ddf by codice.

the class KlvEncodingDetectedString method decodeValue.

@Override
protected void decodeValue(Klv klv) {
    byte[] bytes = klv.getValue();
    CharsetDetector charsetDetector = new CharsetDetector();
    charsetDetector.setText(bytes);
    CharsetMatch[] charsetMatches = charsetDetector.detectAll();
    Optional<CharsetMatch> charsetMatch = Arrays.stream(charsetMatches).filter(match -> possibleCharsets.contains(match.getName())).findFirst();
    Charset charset = utf8;
    if (charsetMatch.isPresent()) {
        try {
            charset = Charset.forName(charsetMatch.get().getName());
        } catch (IllegalArgumentException e) {
            LOGGER.trace("Unsupported encoding, falling back to default encoding");
        }
    }
    value = new String(bytes, charset);
}
Also used : Arrays(java.util.Arrays) List(java.util.List) KlvDataElement(org.codice.ddf.libs.klv.KlvDataElement) Logger(org.slf4j.Logger) Charset(java.nio.charset.Charset) CharsetMatch(org.apache.tika.parser.txt.CharsetMatch) Klv(org.codice.ddf.libs.klv.data.Klv) LoggerFactory(org.slf4j.LoggerFactory) Optional(java.util.Optional) CharsetDetector(org.apache.tika.parser.txt.CharsetDetector) CharsetMatch(org.apache.tika.parser.txt.CharsetMatch) CharsetDetector(org.apache.tika.parser.txt.CharsetDetector) Charset(java.nio.charset.Charset)

Aggregations

Charset (java.nio.charset.Charset)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 CharsetDetector (org.apache.tika.parser.txt.CharsetDetector)1 CharsetMatch (org.apache.tika.parser.txt.CharsetMatch)1 KlvDataElement (org.codice.ddf.libs.klv.KlvDataElement)1 Klv (org.codice.ddf.libs.klv.data.Klv)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1