diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 967b7ae667..e03cec231d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -41,9 +41,6 @@ snakeyaml = { group = "org.yaml", name = "snakeyaml", version = "2.3" } commonslang3 = { group = "org.apache.commons", name = "commons-lang3", version = "3.17.0" } annotations = { group = "org.jetbrains", name = "annotations", version = "26.0.1" } semver4j = { group = "org.semver4j", name = "semver4j", version = "5.4.1" } -# Zlib -# NOTICE: This lib seems won't be used in the future, need to have more checks -libdeflate = { group = "cn.powernukkitx", name = "libdeflate-java", version = "0.0.2-PNX" } # Concurrency disruptor = { group = "com.lmax", name = "disruptor", version = "4.0.0" } # IO diff --git a/server/build.gradle.kts b/server/build.gradle.kts index 17fd0749b9..e85081087f 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -34,7 +34,6 @@ dependencies { implementation(libs.mcterminal) implementation(libs.bundles.logging) implementation(libs.disruptor) - implementation(libs.libdeflate) implementation(libs.bundles.leveldb) implementation(libs.netty.epoll) implementation(libs.netty.kqueue) diff --git a/server/src/main/java/org/allaymc/server/zlib/CompressionType.java b/server/src/main/java/org/allaymc/server/zlib/CompressionType.java deleted file mode 100644 index 79ff29665b..0000000000 --- a/server/src/main/java/org/allaymc/server/zlib/CompressionType.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.allaymc.server.zlib; - -/** - * @author Cool_Loong - */ -public enum CompressionType { - ZLIB, - GZIP -} diff --git a/server/src/main/java/org/allaymc/server/zlib/JavaZibThreadLocal.java b/server/src/main/java/org/allaymc/server/zlib/JavaZibThreadLocal.java deleted file mode 100644 index 47aef03f52..0000000000 --- a/server/src/main/java/org/allaymc/server/zlib/JavaZibThreadLocal.java +++ /dev/null @@ -1,99 +0,0 @@ -package org.allaymc.server.zlib; - -import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream; - -import java.io.IOException; -import java.util.zip.DataFormatException; -import java.util.zip.Deflater; -import java.util.zip.Inflater; - -/** - * @author Cool_Loong - */ -public final class JavaZibThreadLocal implements ZlibProvider { - private static final ThreadLocal FBAO = ThreadLocal.withInitial(() -> new FastByteArrayOutputStream(1024)); - private static final ThreadLocal BUFFER = ThreadLocal.withInitial(() -> new byte[8192]); - private final ThreadLocal INFLATER = ThreadLocal.withInitial(Inflater::new); - private int level; - private final ThreadLocal DEFLATER = ThreadLocal.withInitial(() -> new Deflater(level)); - private CompressionType type; - - JavaZibThreadLocal(CompressionType type, int level) { - this.type = type; - this.level = level; - } - - @Override - public void setCompressionType(CompressionType type) { - this.type = type; - } - - @Override - public void setCompressionLevel(int level) { - this.level = level; - } - - @Override - public byte[] deflate(byte[] data) throws IOException { - try (var bos = FBAO.get()) { - if (type == CompressionType.GZIP) { - throw new UnsupportedOperationException(this.getClass().getSimpleName() + " dont support GZIP"); - } else { - Deflater deflater = DEFLATER.get(); - try { - deflater.reset(); - deflater.setInput(data); - deflater.finish(); - bos.reset(); - byte[] buffer = BUFFER.get(); - int length = 0; - while (!deflater.finished()) { - int i = deflater.deflate(buffer); - bos.write(buffer, 0, i); - length += i; - } - byte[] output = new byte[length]; - System.arraycopy(bos.array, 0, output, 0, length); - return output; - } finally { - deflater.reset(); - } - } - } - } - - @Override - public byte[] inflate(byte[] data, int maxSize) throws IOException { - try (var bos = FBAO.get()) { - if (type == CompressionType.GZIP) { - throw new UnsupportedOperationException(this.getClass().getSimpleName() + " dont support GZIP"); - } else { - Inflater inflater = INFLATER.get(); - try { - inflater.reset(); - inflater.setInput(data); - bos.reset(); - byte[] buffer = BUFFER.get(); - try { - int length = 0; - while (!inflater.finished()) { - int i = inflater.inflate(buffer); - length += i; - if (maxSize > 0 && length > maxSize) { - throw new IOException("Inflated data exceeds maximum size"); - } - bos.write(buffer, 0, i); - } - byte[] output = new byte[length]; - System.arraycopy(bos.array, 0, output, 0, length); - return output; - } catch (DataFormatException e) { - throw new IOException("Unable to inflate zlib stream", e); - } - } finally { - inflater.end(); - } - } - } - } -} diff --git a/server/src/main/java/org/allaymc/server/zlib/LibDeflateThreadLocal.java b/server/src/main/java/org/allaymc/server/zlib/LibDeflateThreadLocal.java deleted file mode 100644 index d8ca1dd7f6..0000000000 --- a/server/src/main/java/org/allaymc/server/zlib/LibDeflateThreadLocal.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.allaymc.server.zlib; - - -import cn.powernukkitx.libdeflate.LibdeflateCompressor; -import cn.powernukkitx.libdeflate.LibdeflateDecompressor; - -import java.io.IOException; -import java.util.zip.DataFormatException; - -/** - * @author Cool_Loong - */ -public final class LibDeflateThreadLocal implements ZlibProvider { - private final ThreadLocal PNX_INFLATER = ThreadLocal.withInitial(LibdeflateDecompressor::new); - private CompressionType type; - private int level; - private final ThreadLocal DEFLATER = ThreadLocal.withInitial(() -> new LibdeflateCompressor(level)); - - LibDeflateThreadLocal(CompressionType type, int level) { - this.type = type; - this.level = level; - } - - @Override - public void setCompressionType(CompressionType type) { - this.type = type; - } - - @Override - public void setCompressionLevel(int level) { - this.level = level; - } - - @Override - public byte[] deflate(byte[] data) throws IOException { - try (LibdeflateCompressor deflater = DEFLATER.get()) { - var t = type == CompressionType.ZLIB ? cn.powernukkitx.libdeflate.CompressionType.ZLIB : cn.powernukkitx.libdeflate.CompressionType.GZIP; - int compressUpperBound = (int) deflater.getCompressBound(data.length, t); - byte[] buffer = new byte[compressUpperBound]; - int compressedSize = deflater.compress(data, buffer, t); - byte[] output = new byte[compressedSize]; - System.arraycopy(buffer, 0, output, 0, compressedSize); - return output; - } catch (Exception e) { - throw new IOException("Deflate error."); - } - } - - - @Override - public byte[] inflate(byte[] data, int maxSize) throws IOException { - var t = type == CompressionType.ZLIB ? cn.powernukkitx.libdeflate.CompressionType.ZLIB : cn.powernukkitx.libdeflate.CompressionType.GZIP; - try (LibdeflateDecompressor pnxInflater = PNX_INFLATER.get()) { - byte[] buffer = new byte[maxSize]; - try { - var result = pnxInflater.decompressUnknownSize(data, 0, data.length, buffer, 0, maxSize, t); - if (maxSize > 0 && result > maxSize) { - throw new IOException("Inflated data exceeds maximum size"); - } - byte[] output = new byte[(int) result]; - System.arraycopy(buffer, 0, output, 0, output.length); - return output; - } catch (DataFormatException e) { - throw new IOException("Unable to inflate zlib stream", e); - } - } - } -} diff --git a/server/src/main/java/org/allaymc/server/zlib/ZlibProvider.java b/server/src/main/java/org/allaymc/server/zlib/ZlibProvider.java deleted file mode 100644 index b33c300898..0000000000 --- a/server/src/main/java/org/allaymc/server/zlib/ZlibProvider.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.allaymc.server.zlib; - -import java.io.IOException; - -/** - * @author Cool_Loong - */ -public interface ZlibProvider { - - /** - * Compress the specified byte array - * - * @param data the array to compress - * - * @return compressed byte array - * - * @throws IOException compress Exception - */ - byte[] deflate(byte[] data) throws IOException; - - /** - * Decompress the specified byte array - * - * @param data the array to be decompressed - * @param maxSize The maximum length of the estimated result byte array - * - * @return decompressed byte array - * - * @throws IOException Decompress Exception - */ - byte[] inflate(byte[] data, int maxSize) throws IOException; - - /** - * set compression level - * - * @param level the level - */ - void setCompressionLevel(int level); - - /** - * Set compression type. - * - * @param type the type - */ - void setCompressionType(CompressionType type); -} diff --git a/server/src/main/java/org/allaymc/server/zlib/ZlibProviderType.java b/server/src/main/java/org/allaymc/server/zlib/ZlibProviderType.java deleted file mode 100644 index b2019c5f9a..0000000000 --- a/server/src/main/java/org/allaymc/server/zlib/ZlibProviderType.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.allaymc.server.zlib; - -/** - * @author Cool_Loong - */ -public enum ZlibProviderType { - LibDeflateThreadLocal, - JavaZlibThreadLocal; - - public ZlibProvider of(CompressionType type, int level) { - return this == ZlibProviderType.LibDeflateThreadLocal ? - new LibDeflateThreadLocal(type, level) : - new JavaZibThreadLocal(type, level); - } -} diff --git a/server/src/test/java/org/allaymc/server/zlib/ZlibTest.java b/server/src/test/java/org/allaymc/server/zlib/ZlibTest.java deleted file mode 100644 index 90e697df41..0000000000 --- a/server/src/test/java/org/allaymc/server/zlib/ZlibTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.allaymc.server.zlib; - -import lombok.SneakyThrows; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.io.IOException; - -/** - * @author Cool_Loong - */ -public class ZlibTest { - final ZlibProvider JAVA_ZLIB = ZlibProviderType.JavaZlibThreadLocal.of(CompressionType.ZLIB, 6); - final ZlibProvider LIB_DEFLATE_ZLIB = ZlibProviderType.LibDeflateThreadLocal.of(CompressionType.ZLIB, 6); - final ZlibProvider LIB_DEFLATE_GZIP = ZlibProviderType.LibDeflateThreadLocal.of(CompressionType.GZIP, 6); - - final byte[] TEST_DATA = new byte[]{122, 1, -23, 34, 123, 35, 65, 78, 91, 51, -12, 32, -4, 5, -65, -123, 12, 32, 45, 94, 123}; - final byte[] HELLO_WORLD_ZLIB = new byte[]{120, -100, -13, 72, -51, -55, -55, 87, 8, -49, 47, -54, 73, 81, 4, 0, 28, 73, 4, 62}; - final byte[] HELLO_WORLD_GZIP = new byte[]{31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 1, 12, 0, -13, -1, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, -93, 28, 41, 28, 12, 0, 0, 0}; - - @SneakyThrows - @Test - void testCompressZlib() { - byte[] deflate = JAVA_ZLIB.deflate(TEST_DATA); - byte[] inflate = JAVA_ZLIB.inflate(deflate, TEST_DATA.length); - Assertions.assertArrayEquals(TEST_DATA, inflate); - - byte[] deflate2 = LIB_DEFLATE_ZLIB.deflate(TEST_DATA); - byte[] inflate2 = LIB_DEFLATE_ZLIB.inflate(deflate2, TEST_DATA.length); - Assertions.assertArrayEquals(TEST_DATA, inflate2); - } - - @SneakyThrows - @Test - void testDecompressZlib() { - byte[] inflate = JAVA_ZLIB.inflate(HELLO_WORLD_ZLIB, HELLO_WORLD_ZLIB.length); - String s = new String(inflate); - Assertions.assertEquals("Hello World!", s); - - byte[] inflate2 = LIB_DEFLATE_ZLIB.inflate(HELLO_WORLD_ZLIB, HELLO_WORLD_ZLIB.length); - String s2 = new String(inflate2); - Assertions.assertEquals("Hello World!", s2); - } - - @SneakyThrows - @Test - void testCompressGZip() { - byte[] deflate = LIB_DEFLATE_GZIP.deflate(TEST_DATA); - byte[] inflate = LIB_DEFLATE_GZIP.inflate(deflate, TEST_DATA.length); - Assertions.assertArrayEquals(TEST_DATA, inflate); - } - - @SneakyThrows - @Test - void testDecompressGZip() { - byte[] inflate = LIB_DEFLATE_GZIP.inflate(HELLO_WORLD_GZIP, HELLO_WORLD_GZIP.length); - String s = new String(inflate); - Assertions.assertEquals("Hello World!", s); - } -}