Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8267551: Support loading images from inline data-URIs #508

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions modules/javafx.graphics/src/main/java/javafx/scene/image/Image.java
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@

package javafx.scene.image;

import java.io.File;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
@@ -610,15 +611,18 @@ public String getName() {
* Constructs an {@code Image} with content loaded from the specified
* image file. The {@code url} parameter can be one of the following:
* <ol>
* <li>a file path that is a valid argument for {@link java.io.File#File(String)}
* <li>a URL that is a valid argument for {@link java.net.URL#URL(String)}
* <li>the name of a resource that can be resolved by the context
* {@link ClassLoader} for this thread
* <li>a file path that can be resolved by {@link java.io.File}
* <li>a URL that can be resolved by {@link java.net.URL} and for
* which a protocol handler exists
* </ol>
* The RFC 2397 "data" scheme for URLs is supported in addition to
* the protocol handlers that are registered for the application.
* <p>
* If a URL uses the "data" scheme, the data must be base64-encoded
* and the MIME type must either be empty or a subtype of the
* {@code image/} type.
* {@code image} type.
*
* @param url a file path or URL
* @throws NullPointerException if {@code url} is null
@@ -633,15 +637,18 @@ public Image(@NamedArg("url") String url) {
* Constructs an {@code Image} with content loaded from the specified
* image file. The {@code url} parameter can be one of the following:
* <ol>
* <li>a file path that is a valid argument for {@link java.io.File#File(String)}
* <li>a URL that is a valid argument for {@link java.net.URL#URL(String)}
* <li>the name of a resource that can be resolved by the context
* {@link ClassLoader} for this thread
* <li>a file path that can be resolved by {@link java.io.File}
* <li>a URL that can be resolved by {@link java.net.URL} and for
* which a protocol handler exists
* </ol>
* The RFC 2397 "data" scheme for URLs is supported in addition to
* the protocol handlers that are registered for the application.
* <p>
* If a URL uses the "data" scheme, the data must be base64-encoded
* and the MIME type must either be empty or a subtype of the
* {@code image/} type.
* {@code image} type.
*
* @param url a file path or URL
* @param backgroundLoading indicates whether the image
@@ -658,15 +665,18 @@ public Image(@NamedArg("url") String url, @NamedArg("backgroundLoading") boolean
* Constructs an {@code Image} with content loaded from the specified
* image file. The {@code url} parameter can be one of the following:
* <ol>
* <li>a file path that is a valid argument for {@link java.io.File#File(String)}
* <li>a URL that is a valid argument for {@link java.net.URL#URL(String)}
* <li>the name of a resource that can be resolved by the context
* {@link ClassLoader} for this thread
* <li>a file path that can be resolved by {@link java.io.File}
* <li>a URL that can be resolved by {@link java.net.URL} and for
* which a protocol handler exists
* </ol>
* The RFC 2397 "data" scheme for URLs is supported in addition to
* the protocol handlers that are registered for the application.
* <p>
* If a URL uses the "data" scheme, the data must be base64-encoded
* and the MIME type must either be empty or a subtype of the
* {@code image/} type.
* {@code image} type.
*
* @param url a file path or URL
* @param requestedWidth the image's bounding box width
@@ -691,15 +701,18 @@ public Image(@NamedArg("url") String url, @NamedArg("requestedWidth") double req
* Constructs an {@code Image} with content loaded from the specified
* image file. The {@code url} parameter can be one of the following:
* <ol>
* <li>a file path that is a valid argument for {@link java.io.File#File(String)}
* <li>a URL that is a valid argument for {@link java.net.URL#URL(String)}
* <li>the name of a resource that can be resolved by the context
* {@link ClassLoader} for this thread
* <li>a file path that can be resolved by {@link java.io.File}
* <li>a URL that can be resolved by {@link java.net.URL} and for
* which a protocol handler exists
* </ol>
* The RFC 2397 "data" scheme for URLs is supported in addition to
* the protocol handlers that are registered for the application.
* <p>
* If a URL uses the "data" scheme, the data must be base64-encoded
* and the MIME type must either be empty or a subtype of the
* {@code image/} type.
* {@code image} type.
*
* @param url a file path or URL
* @param requestedWidth the image's bounding box width
@@ -1156,6 +1169,11 @@ private static String validateUrl(final String url) {
} else if (DataURI.matchScheme(url)) {
return url;
}

if (new File(url).exists()) {
return url;
}

// Use URL constructor for validation
return new URL(url).toString();
} catch (final IllegalArgumentException | MalformedURLException e) {