Mirror von
https://github.com/PaperMC/Velocity.git
synchronisiert 2024-11-17 05:20:14 +01:00
Remove dependency on the java.desktop module
The Favicon.create(BufferedImage) method has been removed. In its place we have Favicon.create(Path) (carried over from the 1.x.x series) and Favicon.create(byte[]) (brand new).
Dieser Commit ist enthalten in:
Ursprung
f61b187b3d
Commit
ef7f4871b8
@ -1,15 +1,11 @@
|
|||||||
package com.velocitypowered.api.util;
|
package com.velocitypowered.api.util;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -65,23 +61,17 @@ public final class Favicon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@code Favicon} from the specified {@code image}.
|
* Creates a new {@code Favicon} from the specified {@code buffer}.
|
||||||
*
|
*
|
||||||
* @param image the image to use for the favicon
|
* @param buffer the buffer to use for the favicon
|
||||||
* @return the created {@link Favicon} instance
|
* @return the created {@link Favicon} instance
|
||||||
|
* @throws IOException if the file could not be read from the path
|
||||||
*/
|
*/
|
||||||
public static Favicon create(BufferedImage image) {
|
public static Favicon create(byte[] buffer) throws IOException {
|
||||||
Preconditions.checkNotNull(image, "image");
|
if (!FaviconChecker.check(buffer)) {
|
||||||
Preconditions.checkArgument(image.getWidth() == 64 && image.getHeight() == 64,
|
throw new IllegalArgumentException("Image is not a PNG file or does not have 64x64 dimensions");
|
||||||
"Image is not 64x64 (found %sx%s)", image.getWidth(),image.getHeight());
|
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
||||||
try {
|
|
||||||
ImageIO.write(image, "PNG", os);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new AssertionError(e);
|
|
||||||
}
|
}
|
||||||
return new Favicon(
|
return new Favicon("data:image/png;base64," + Base64.getEncoder().encodeToString(buffer));
|
||||||
"data:image/png;base64," + Base64.getEncoder().encodeToString(os.toByteArray()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,12 +82,6 @@ public final class Favicon {
|
|||||||
* @throws IOException if the file could not be read from the path
|
* @throws IOException if the file could not be read from the path
|
||||||
*/
|
*/
|
||||||
public static Favicon create(Path path) throws IOException {
|
public static Favicon create(Path path) throws IOException {
|
||||||
try (InputStream stream = Files.newInputStream(path)) {
|
return create(Files.readAllBytes(path));
|
||||||
BufferedImage image = ImageIO.read(stream);
|
|
||||||
if (image == null) {
|
|
||||||
throw new IOException("Unable to read the image.");
|
|
||||||
}
|
|
||||||
return create(image);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
api/src/main/java/com/velocitypowered/api/util/FaviconChecker.java
Normale Datei
34
api/src/main/java/com/velocitypowered/api/util/FaviconChecker.java
Normale Datei
@ -0,0 +1,34 @@
|
|||||||
|
package com.velocitypowered.api.util;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
class FaviconChecker {
|
||||||
|
private static final byte[] PNG_MAGIC = new byte[] {
|
||||||
|
(byte) 137, 80, 78, 71, 13, 10, 26, 10
|
||||||
|
};
|
||||||
|
private static final byte[] IHDR_NAME = new byte[] {
|
||||||
|
73, 72, 68, 82
|
||||||
|
};
|
||||||
|
|
||||||
|
public static boolean check(byte[] data) {
|
||||||
|
ByteBuffer buf = ByteBuffer.wrap(data);
|
||||||
|
|
||||||
|
for (byte value : PNG_MAGIC) {
|
||||||
|
if (buf.get() != value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.position(buf.position() + 4);
|
||||||
|
for (byte value : IHDR_NAME) {
|
||||||
|
if (buf.get() != value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int width = buf.getInt();
|
||||||
|
int height = buf.getInt();
|
||||||
|
|
||||||
|
return width == 64 && height == 64;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.velocitypowered.api.util;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import com.google.common.io.Resources;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class FaviconCheckerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void handlesProperlyFormattedFavicon() throws IOException {
|
||||||
|
assertTrue(FaviconChecker.check(readFile("test_icon_dimensions_correct.png")),
|
||||||
|
"Correctly formatted favicon is not valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void handlesBadDimensionFavicon() throws IOException {
|
||||||
|
assertFalse(FaviconChecker.check(readFile("test_icon_dimensions_wrong.png")),
|
||||||
|
"Incorrect dimension favicon is valid?");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void handlesBadFormatFavicon() throws IOException {
|
||||||
|
assertFalse(FaviconChecker.check(readFile("test_icon_dimensions_wrong_format.jpg")),
|
||||||
|
"Incorrect format favicon is valid?");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] readFile(String path) throws IOException {
|
||||||
|
return Resources.toByteArray(FaviconCheckerTest.class.getResource(path));
|
||||||
|
}
|
||||||
|
}
|
Binäre Datei nicht angezeigt.
Nachher Breite: | Höhe: | Größe: 1.8 KiB |
Binäre Datei nicht angezeigt.
Nachher Breite: | Höhe: | Größe: 4.5 KiB |
Binäre Datei nicht angezeigt.
Nachher Breite: | Höhe: | Größe: 2.9 KiB |
@ -11,10 +11,6 @@ public class Velocity {
|
|||||||
private static final Logger logger = LogManager.getLogger(Velocity.class);
|
private static final Logger logger = LogManager.getLogger(Velocity.class);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// We use BufferedImage for favicons, and on macOS this puts the Java application in the dock.
|
|
||||||
// How inconvenient. Force AWT to work with its head chopped off.
|
|
||||||
System.setProperty("java.awt.headless", "true");
|
|
||||||
|
|
||||||
// By default, Netty allocates 16MiB arenas for the PooledByteBufAllocator. This is too much
|
// By default, Netty allocates 16MiB arenas for the PooledByteBufAllocator. This is too much
|
||||||
// memory for Minecraft, which imposes a maximum packet size of 2MiB! We'll use 4MiB as a more
|
// memory for Minecraft, which imposes a maximum packet size of 2MiB! We'll use 4MiB as a more
|
||||||
// sane default.
|
// sane default.
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren