lingochamp/FileDownloader · error · RuntimeException

Huh, UTF-8 should be supported?

Error message

Huh, UTF-8 should be supported?

What it means

md5() encodes its input as UTF-8 before hashing and wraps UnsupportedEncodingException in a RuntimeException. Like MD5, UTF-8 is guaranteed on every Java/Android runtime, so this is a defensive guard that should never fire in practice. If it does, the platform charset support is broken.

Solutions

  1. Fix the runtime so the standard charsets are available (restore default charset providers).
  2. If running on an exotic JVM, verify Charset.isSupported("UTF-8") and repair the charset provider configuration.
  3. As a fallback in app code, catch the RuntimeException and use string.getBytes(StandardCharsets.UTF_8) via your own hashing routine.

Example fix

// before
String name = FileDownloadUtils.generateFileName(url); // wraps UnsupportedEncodingException

// after (own equivalent, no checked charset lookup)
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hash = md.digest(url.getBytes(StandardCharsets.UTF_8));
Defensive patterns

Strategy: try-catch

Validate before calling

if (!Charset.isSupported("UTF-8")) {
    throw new IllegalStateException("Runtime is missing UTF-8 charset; FileDownloader md5 will fail");
}

Try / catch

try {
    name = FileDownloadUtils.generateFileName(url);
} catch (RuntimeException e) {
    Log.e("FileDownloader", "UTF-8 charset unavailable", e);
    name = String.valueOf(url.hashCode());
}

Prevention

When it happens

Trigger: string.getBytes("UTF-8") throws UnsupportedEncodingException during generateFileName — only possible on a JVM missing the UTF-8 charset, which the spec mandates.

Common situations: Extremely stripped custom runtimes; broken CharsetProvider registrations on embedded/non-standard JVMs. Essentially never seen on stock Android.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of lingochamp/FileDownloader@6237a8cac1 (2026-09-08). Data as JSON: /api/errors/e0b15ad09e7e21ef. Report an issue: GitHub.

Appendix: source

Thrown at library/src/main/java/com/liulishuo/filedownloader/util/FileDownloadUtils.java:236

     *             directory to place the file;
     *             If {@code pathAsDirectory} is {@code false}, {@code path} would be the absolute
     *             file path.
     * @return The download id.
     */
    public static int generateId(final String url, final String path,
                                 final boolean pathAsDirectory) {
        return CustomComponentHolder.getImpl().getIdGeneratorInstance()
                .generateId(url, path, pathAsDirectory);
    }

    public static String md5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }


    public static String getStack() {
        return getStack(true);
    }

    public static String getStack(final boolean printLine) {
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        return getStack(stackTrace, printLine);

View on GitHub (pinned to 6237a8cac1)