Improvements for natives.
This commit is contained in:
@@ -8,13 +8,15 @@ import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
public class JavaVelocityCompressor implements VelocityCompressor {
|
||||
public static final VelocityCompressorFactory FACTORY = JavaVelocityCompressor::new;
|
||||
|
||||
private final Deflater deflater;
|
||||
private final Inflater inflater;
|
||||
private final byte[] buf;
|
||||
private boolean disposed = false;
|
||||
|
||||
public JavaVelocityCompressor() {
|
||||
this.deflater = new Deflater();
|
||||
private JavaVelocityCompressor(int level) {
|
||||
this.deflater = new Deflater(level);
|
||||
this.inflater = new Inflater();
|
||||
this.buf = new byte[ZLIB_BUFFER_SIZE];
|
||||
}
|
||||
|
@@ -4,18 +4,19 @@ import com.google.common.base.Preconditions;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
public class NativeVelocityCompressor implements VelocityCompressor {
|
||||
public static final VelocityCompressorFactory FACTORY = NativeVelocityCompressor::new;
|
||||
|
||||
private final NativeZlibInflate inflate = new NativeZlibInflate();
|
||||
private final long inflateCtx;
|
||||
private final NativeZlibDeflate deflate = new NativeZlibDeflate();
|
||||
private final long deflateCtx;
|
||||
private boolean disposed = false;
|
||||
|
||||
public NativeVelocityCompressor() {
|
||||
private NativeVelocityCompressor(int level) {
|
||||
this.inflateCtx = inflate.init();
|
||||
this.deflateCtx = deflate.init(Deflater.DEFAULT_COMPRESSION);
|
||||
this.deflateCtx = deflate.init(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,5 @@
|
||||
package com.velocitypowered.natives.compression;
|
||||
|
||||
public interface VelocityCompressorFactory {
|
||||
VelocityCompressor create(int level);
|
||||
}
|
@@ -19,7 +19,7 @@ public class NativeCodeLoader<T> implements Supplier<T> {
|
||||
if (selected == null) {
|
||||
selected = select();
|
||||
}
|
||||
return selected.supplier.get();
|
||||
return selected.object;
|
||||
}
|
||||
|
||||
private Variant<T> select() {
|
||||
@@ -44,14 +44,14 @@ public class NativeCodeLoader<T> implements Supplier<T> {
|
||||
private boolean available;
|
||||
private final Runnable setup;
|
||||
private final String name;
|
||||
private final Supplier<T> supplier;
|
||||
private final T object;
|
||||
private boolean hasBeenSetup = false;
|
||||
|
||||
Variant(BooleanSupplier available, Runnable setup, String name, Supplier<T> supplier) {
|
||||
Variant(BooleanSupplier available, Runnable setup, String name, T object) {
|
||||
this.available = available.getAsBoolean();
|
||||
this.setup = setup;
|
||||
this.name = name;
|
||||
this.supplier = supplier;
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
private void setup() {
|
||||
@@ -71,7 +71,7 @@ public class NativeCodeLoader<T> implements Supplier<T> {
|
||||
}
|
||||
|
||||
if (available) {
|
||||
return supplier.get();
|
||||
return object;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
|
||||
import com.velocitypowered.natives.compression.JavaVelocityCompressor;
|
||||
import com.velocitypowered.natives.compression.NativeVelocityCompressor;
|
||||
import com.velocitypowered.natives.compression.VelocityCompressor;
|
||||
import com.velocitypowered.natives.compression.VelocityCompressorFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -34,15 +35,15 @@ public class Natives {
|
||||
};
|
||||
}
|
||||
|
||||
public static final NativeCodeLoader<VelocityCompressor> compressor = new NativeCodeLoader<>(
|
||||
public static final NativeCodeLoader<VelocityCompressorFactory> compressor = new NativeCodeLoader<>(
|
||||
ImmutableList.of(
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.MACOS,
|
||||
copyAndLoadNative("/macosx/velocity-compress.dylib"), "native compression (macOS)",
|
||||
NativeVelocityCompressor::new),
|
||||
NativeVelocityCompressor.FACTORY),
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.LINUX,
|
||||
copyAndLoadNative("/linux_x64/velocity-compress.so"), "native compression (Linux amd64)",
|
||||
NativeVelocityCompressor::new),
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor::new)
|
||||
NativeVelocityCompressor.FACTORY),
|
||||
new NativeCodeLoader.Variant<>(NativeCodeLoader.ALWAYS, () -> {}, "Java compression", JavaVelocityCompressor.FACTORY)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@@ -11,6 +11,7 @@ import org.junit.jupiter.api.condition.EnabledOnOs;
|
||||
import java.util.Random;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
@@ -26,7 +27,7 @@ class VelocityCompressorTest {
|
||||
@Test
|
||||
@EnabledOnOs({ MAC, LINUX })
|
||||
void nativeIntegrityCheck() throws DataFormatException {
|
||||
VelocityCompressor compressor = Natives.compressor.get();
|
||||
VelocityCompressor compressor = Natives.compressor.get().create(Deflater.DEFAULT_COMPRESSION);
|
||||
if (compressor instanceof JavaVelocityCompressor) {
|
||||
fail("Loaded regular compressor");
|
||||
}
|
||||
@@ -35,7 +36,7 @@ class VelocityCompressorTest {
|
||||
|
||||
@Test
|
||||
void javaIntegrityCheck() throws DataFormatException {
|
||||
JavaVelocityCompressor compressor = new JavaVelocityCompressor();
|
||||
VelocityCompressor compressor = JavaVelocityCompressor.FACTORY.create(Deflater.DEFAULT_COMPRESSION);
|
||||
check(compressor);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user