diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/HMCLURLStreamHandlerProvider.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/HMCLURLStreamHandlerProvider.java new file mode 100644 index 000000000..3c5846f1c --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/HMCLURLStreamHandlerProvider.java @@ -0,0 +1,37 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.util.url; + +import org.jackhuang.hmcl.util.url.data.DataURLHandle; + +import java.net.URLStreamHandler; +import java.net.spi.URLStreamHandlerProvider; + +/** + * @author Glavo + */ +public final class HMCLURLStreamHandlerProvider extends URLStreamHandlerProvider { + @Override + public URLStreamHandler createURLStreamHandler(String protocol) { + switch (protocol) { + case "data": + return new DataURLHandle(); + } + return null; + } +} diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLConnection.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLConnection.java new file mode 100644 index 000000000..642d5da65 --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLConnection.java @@ -0,0 +1,63 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.util.url.data; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +/** + * @author Glavo + */ +public final class DataURLConnection extends URLConnection { + private final DataUri dataUri; + private byte[] data; + private InputStream inputStream; + + DataURLConnection(URL url, DataUri dataUri) { + super(url); + this.dataUri = dataUri; + } + + @Override + public void connect() throws IOException { + if (!connected) { + connected = true; + data = dataUri.readBytes(); + inputStream = new ByteArrayInputStream(this.data); + } + } + + @Override + public InputStream getInputStream() throws IOException { + connect(); + return inputStream; + } + + @Override + public String getContentType() { + return dataUri.getMediaType(); + } + + @Override + public long getContentLengthLong() { + return data != null ? data.length : -1; + } +} diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLHandle.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLHandle.java new file mode 100644 index 000000000..66559ba15 --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataURLHandle.java @@ -0,0 +1,39 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.util.url.data; + +import java.io.IOException; +import java.net.*; + +/** + * @author Glavo + */ +public final class DataURLHandle extends URLStreamHandler { + @Override + protected URLConnection openConnection(URL u) throws IOException { + try { + URI uri = u.toURI(); + if (!DataUri.isDataUri(uri)) + throw new MalformedURLException("URI is not a data URI: " + u); + + return new DataURLConnection(u, new DataUri(uri)); + } catch (URISyntaxException e) { + throw new MalformedURLException(e.getMessage()); + } + } +} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/DataUri.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java similarity index 96% rename from HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/DataUri.java rename to HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java index 5ace39847..56c46871e 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/DataUri.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/url/data/DataUri.java @@ -15,8 +15,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.jackhuang.hmcl.util.io; +package org.jackhuang.hmcl.util.url.data; +import org.jackhuang.hmcl.util.io.NetworkUtils; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -24,6 +25,9 @@ import java.net.URI; import java.nio.charset.Charset; import java.util.Base64; +/** + * @author Glavo + */ public final class DataUri { public static final String SCHEME = "data"; @@ -58,7 +62,6 @@ public final class DataUri { if (base64) mediaType = mediaType.substring(0, mediaType.length() - ";base64".length()); - this.mediaType = mediaType.trim(); this.charset = NetworkUtils.getCharsetFromContentType(mediaType); this.base64 = base64; diff --git a/HMCL/src/main/resources/META-INF/services/java.net.spi.URLStreamHandlerProvider b/HMCL/src/main/resources/META-INF/services/java.net.spi.URLStreamHandlerProvider new file mode 100644 index 000000000..841af2992 --- /dev/null +++ b/HMCL/src/main/resources/META-INF/services/java.net.spi.URLStreamHandlerProvider @@ -0,0 +1 @@ +org.jackhuang.hmcl.util.url.HMCLURLStreamHandlerProvider \ No newline at end of file diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/DataUriTest.java b/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataUriTest.java similarity index 97% rename from HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/DataUriTest.java rename to HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataUriTest.java index 7b43ccbdd..e1447e031 100644 --- a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/io/DataUriTest.java +++ b/HMCL/src/test/java/org/jackhuang/hmcl/util/url/data/DataUriTest.java @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.jackhuang.hmcl.util.io; +package org.jackhuang.hmcl.util.url.data; import org.junit.jupiter.api.Test;