apache/pulsar · error · MalformedURLException

(no message; malformed data: URL)

Error message

(no message; malformed data: URL)

What it means

After connect() obtains the URI, it validates the scheme-specific part against a regex that captures optional mime type, base64 flag, and payload. If the data: URL does not match the expected grammar (e.g. missing comma separator, invalid base64, stray characters), it throws MalformedURLException.

Source

Thrown at pulsar-common/src/main/java/org/apache/pulsar/client/api/url/DataURLStreamHandler.java:85

            if (this.uri == null) {
                throw new IOException();
            }

            Matcher matcher = pattern.matcher(this.uri.getSchemeSpecificPart());
            if (matcher.matches()) {
                this.contentType = matcher.group("mimeType");
                if (contentType == null) {
                    this.contentType = "application/data";
                }

                if (matcher.group("base64") == null) {
                    // Support Urlencode but not decode here because already decoded by URI class.
                    this.data = matcher.group("data").getBytes(StandardCharsets.UTF_8);
                } else {
                    this.data = Base64.getDecoder().decode(matcher.group("data"));
                }
            } else {
                throw new MalformedURLException();
            }
            parsed = true;
        }

        @Override
        public long getContentLengthLong() {
            long length;
            try {
                this.connect();
                length = this.data.length;
            } catch (IOException e) {
                length = -1;
            }
            return length;
        }

        @Override
        public String getContentType() {

View on GitHub (pinned to 820761864e)

Solutions

  1. Validate the data URL format before use: must contain a ',' separating metadata from payload, e.g. data:text/plain;base64,SGVsbG8=.
  2. If the payload is base64, ensure it is valid base64 (correct padding, no whitespace/newlines); otherwise use the plain (URL-encoded) form.
  3. Catch MalformedURLException and log/inspect the full URI to spot the malformed part; fix or regenerate the URL string.
  4. Percent-encode special characters in the data portion instead of embedding raw binary or spaces.

Example fix

// before
String bad = "data:text/plain;base64" + payload; // missing comma
URL url = new URL(null, bad, new DataURLStreamHandler());
// after
String good = "data:text/plain;base64," + Base64.getEncoder().encodeToString(bytes);
URL url = new URL(null, good, new DataURLStreamHandler());
Defensive patterns

Strategy: validation

Validate before calling

int comma = urlString.indexOf(',');
if (!urlString.startsWith("data:") || comma < 5) {
    throw new IllegalArgumentException("malformed data URL, missing metadata/payload comma: " + urlString);
}

Try / catch

try {
    URLConnection c = dataUrl.openConnection(); c.connect();
} catch (MalformedURLException e) {
    // data URL didn't match grammar; inspect and rebuild the URL
}

Prevention

When it happens

Trigger: Calling getInputStream/getContentType/getContentLengthLong on a connection whose data: URI doesn't match pattern 'data:[<mediatype>][;base64],<data>' — missing comma, whitespace, unescaped characters, or invalid base64 payload.

Common situations: Hand-built data URLs missing the comma before payload; base64 payloads containing newlines/padding errors; URLs that were double-encoded or contain spaces; truncation when building the URL string.

Understand the failure class

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/e071c964c8de45e5. Report an issue: GitHub.