private static String decodeWord()

in commons-fileupload2-core/src/main/java/org/apache/commons/fileupload2/core/MimeUtils.java [183:241]


    private static String decodeWord(final String word) throws ParseException, UnsupportedEncodingException {
        // encoded words start with the characters "=?". If this not an encoded word, we throw a
        // ParseException for the caller.

        final var etmPos = word.indexOf(ENCODED_TOKEN_MARKER);
        if (etmPos != 0) {
            throw new ParseException("Invalid RFC 2047 encoded-word: " + word, etmPos);
        }

        final var charsetPos = word.indexOf('?', 2);
        if (charsetPos == -1) {
            throw new ParseException("Missing charset in RFC 2047 encoded-word: " + word, charsetPos);
        }

        // pull out the character set information (this is the MIME name at this point).
        final var charset = word.substring(2, charsetPos).toLowerCase(Locale.ROOT);

        // now pull out the encoding token the same way.
        final var encodingPos = word.indexOf('?', charsetPos + 1);
        if (encodingPos == -1) {
            throw new ParseException("Missing encoding in RFC 2047 encoded-word: " + word, encodingPos);
        }

        final var encoding = word.substring(charsetPos + 1, encodingPos);

        // and finally the encoded text.
        final var encodedTextPos = word.indexOf(ENCODED_TOKEN_FINISHER, encodingPos + 1);
        if (encodedTextPos == -1) {
            throw new ParseException("Missing encoded text in RFC 2047 encoded-word: " + word, encodedTextPos);
        }

        final var encodedText = word.substring(encodingPos + 1, encodedTextPos);

        // seems a bit silly to encode a null string, but easy to deal with.
        if (encodedText.isEmpty()) {
            return "";
        }

        try {
            // the decoder writes directly to an output stream.
            final var out = new ByteArrayOutputStream(encodedText.length());

            final var encodedData = encodedText.getBytes(StandardCharsets.US_ASCII);

            // Base64 encoded?
            if (encoding.equals(BASE64_ENCODING_MARKER)) {
                out.write(Base64.getMimeDecoder().decode(encodedData));
            } else if (encoding.equals(QUOTEDPRINTABLE_ENCODING_MARKER)) { // maybe quoted printable.
                QuotedPrintableDecoder.decode(encodedData, out);
            } else {
                throw new UnsupportedEncodingException("Unknown RFC 2047 encoding: " + encoding);
            }
            // get the decoded byte data and convert into a string.
            final var decodedData = out.toByteArray();
            return new String(decodedData, javaCharset(charset));
        } catch (final IOException e) {
            throw new UnsupportedEncodingException("Invalid RFC 2047 encoding");
        }
    }