HMCL-dev/HMCL · error · IllegalArgumentException
Invalid data URI
Error message
Invalid data URI: ${uri} What it means
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>").
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.
Example fix
// before
new DataUri(URI.create("data:text/plain"))
// after
new DataUri(URI.create("data:text/plain,hello")) Defensive patterns
Strategy: validation
Validate before calling
boolean isValidDataUri(URI uri) {
return uri != null
&& "data".equals(uri.getScheme())
&& uri.getSchemeSpecificPart() != null
&& uri.getSchemeSpecificPart().indexOf(',') >= 0;
}
// if (!isValidDataUri(uri)) throw new IllegalArgumentException("not a data URI: " + uri); Type guard
Optional<DataUri> tryParseDataUri(URI uri) {
try { return Optional.of(new DataUri(uri)); }
catch (IllegalArgumentException e) { return Optional.empty(); }
} Try / catch
try {
DataUri d = new DataUri(uri);
} catch (IllegalArgumentException e) {
LOG.warn("Rejecting invalid data URI: " + uri, e);
// fall back to file-based resource or skip asset
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.
Related errors
- bad query string
- name existing
- Texture url is empty
- Failed to download texture
- Platform is mismatch: expected
AI-assisted analysis of HMCL-dev/HMCL@24702dc5a0 (2026-09-10).
Data as JSON: /api/errors/1afacfc1b4aa72e7.
Report an issue: GitHub.
Appendix: source
Thrown at HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java:54
}
public static boolean isDataUri(URI uri) {
return uri != null && SCHEME.equals(uri.getScheme());
}
private final @NotNull String mediaType;
private final @NotNull Charset charset;
private final boolean base64;
private final @NotNull String rawData;
public DataUri(URI uri) {
if (!uri.getScheme().equals(SCHEME)) {
throw new IllegalArgumentException("URI scheme must be " + SCHEME);
}
String schemeSpecificPart = uri.getSchemeSpecificPart();
if (schemeSpecificPart == null)
throw invalidUri(uri);
int comma = schemeSpecificPart.indexOf(',');
if (comma < 0)
throw invalidUri(uri);
String mediaType = schemeSpecificPart.substring(0, comma);
boolean base64 = mediaType.endsWith(";base64");
if (base64)
mediaType = mediaType.substring(0, mediaType.length() - ";base64".length());
this.mediaType = mediaType.trim();
this.charset = NetworkUtils.getCharsetFromContentType(mediaType);
this.base64 = base64;
this.rawData = schemeSpecificPart.substring(comma + 1);
}
public @NotNull String getMediaType() {
return mediaType;View on GitHub (pinned to 24702dc5a0)