{"record":{"id":"1afacfc1b4aa72e7","repo":"HMCL-dev/HMCL","slug":"invalid-data-uri-uri","errorCode":null,"errorMessage":"Invalid data URI: ${uri}","messagePattern":"Invalid data URI: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java","lineNumber":54,"sourceCode":"    }\n\n    public static boolean isDataUri(URI uri) {\n        return uri != null && SCHEME.equals(uri.getScheme());\n    }\n\n    private final @NotNull String mediaType;\n    private final @NotNull Charset charset;\n    private final boolean base64;\n    private final @NotNull String rawData;\n\n    public DataUri(URI uri) {\n        if (!uri.getScheme().equals(SCHEME)) {\n            throw new IllegalArgumentException(\"URI scheme must be \" + SCHEME);\n        }\n\n        String schemeSpecificPart = uri.getSchemeSpecificPart();\n        if (schemeSpecificPart == null)\n            throw invalidUri(uri);\n\n        int comma = schemeSpecificPart.indexOf(',');\n        if (comma < 0)\n            throw invalidUri(uri);\n\n        String mediaType = schemeSpecificPart.substring(0, comma);\n        boolean base64 = mediaType.endsWith(\";base64\");\n        if (base64)\n            mediaType = mediaType.substring(0, mediaType.length() - \";base64\".length());\n\n        this.mediaType = mediaType.trim();\n        this.charset = NetworkUtils.getCharsetFromContentType(mediaType);\n        this.base64 = base64;\n        this.rawData = schemeSpecificPart.substring(comma + 1);\n    }\n\n    public @NotNull String getMediaType() {\n        return mediaType;","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java#L36-L72","documentation":"DataUri's constructor validates that the URI uses the 'data:' scheme and that its scheme-specific part is non-null and contains a ',' separating media type from payload. When the scheme-specific part is null or there is no comma, invalidUri(uri) throws IllegalArgumentException(\"Invalid data URI: <uri>\").","triggerScenarios":"Constructing new DataUri(uri) where uri.getSchemeSpecificPart() returns null (opaque URI edge cases), or where the part after 'data:' contains no ',' (e.g. 'data:text/plain' with no payload separator).","commonSituations":"Assets or config entries referencing 'data:' URIs that were truncated by copy/paste or template substitution; hand-written data URIs missing the comma; URIs reconstructed programmatically and losing the opaque part.","solutions":["Include the ',' separator and payload in the data URI (e.g. 'data:text/plain,hello').","Verify the URI string is complete after variable/template substitution.","Pre-validate with URI.create(uri) and check getSchemeSpecificPart() contains ',' before constructing DataUri."],"exampleFix":"// before\nnew DataUri(URI.create(\"data:text/plain\"))\n// after\nnew DataUri(URI.create(\"data:text/plain,hello\"))","handlingStrategy":"validation","validationCode":"boolean isValidDataUri(URI uri) {\n    return uri != null\n        && \"data\".equals(uri.getScheme())\n        && uri.getSchemeSpecificPart() != null\n        && uri.getSchemeSpecificPart().indexOf(',') >= 0;\n}\n// if (!isValidDataUri(uri)) throw new IllegalArgumentException(\"not a data URI: \" + uri);","typeGuard":"Optional<DataUri> tryParseDataUri(URI uri) {\n    try { return Optional.of(new DataUri(uri)); }\n    catch (IllegalArgumentException e) { return Optional.empty(); }\n}","tryCatchPattern":"try {\n    DataUri d = new DataUri(uri);\n} catch (IllegalArgumentException e) {\n    LOG.warn(\"Rejecting invalid data URI: \" + uri, e);\n    // fall back to file-based resource or skip asset\n}","preventionTips":["Always build data URIs as 'data:<mediaType>[;base64],<payload>' including the comma.","Generate URIs with a helper instead of string concatenation.","Check for truncation after template/variable substitution.","Round-trip test: serialize then parse the URI."],"tags":["data-uri","url-parsing","java"],"backgroundTag":"invalid-url-format","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}