Cleaned up native code.

This commit is contained in:
Andrew Steinborn
2018-08-02 00:43:38 -04:00
parent 850b2f7958
commit 2e59138428
11 changed files with 33 additions and 25 deletions

View File

@@ -51,6 +51,8 @@ public class JavaVelocityCompressor implements VelocityCompressor {
@Override
public void dispose() {
disposed = true;
deflater.end();
inflater.end();
}
private void ensureNotDisposed() {

View File

@@ -20,5 +20,5 @@ class NativeZlibDeflate {
initIDs();
}
static native void initIDs();
private static native void initIDs();
}

View File

@@ -3,7 +3,7 @@ package com.velocitypowered.natives.compression;
/**
* Represents a native interface for zlib's inflate functions.
*/
public class NativeZlibInflate {
class NativeZlibInflate {
boolean finished;
int consumed;
@@ -19,5 +19,5 @@ public class NativeZlibInflate {
initIDs();
}
static native void initIDs();
private static native void initIDs();
}

View File

@@ -5,7 +5,13 @@ import io.netty.buffer.ByteBuf;
import java.util.zip.DataFormatException;
/**
* Provides an interface to inflate and deflate {@link ByteBuf}s using zlib.
*/
public interface VelocityCompressor extends Disposable {
/**
* The default preferred output buffer size for zlib.
*/
int ZLIB_BUFFER_SIZE = 8192;
void inflate(ByteBuf source, ByteBuf destination) throws DataFormatException;

View File

@@ -6,7 +6,7 @@ import java.util.List;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
public class NativeCodeLoader<T> {
public class NativeCodeLoader<T> implements Supplier<T> {
private final List<Variant<T>> variants;
private Variant<T> selected;
@@ -14,11 +14,12 @@ public class NativeCodeLoader<T> {
this.variants = ImmutableList.copyOf(variants);
}
public Supplier<T> supply() {
@Override
public T get() {
if (selected == null) {
selected = select();
}
return selected.supplier;
return selected.supplier.get();
}
private Variant<T> select() {
@@ -33,14 +34,10 @@ public class NativeCodeLoader<T> {
}
public String getLoadedVariant() {
for (Variant<T> variant : variants) {
T got = variant.get();
if (got == null) {
continue;
}
return variant.name;
if (selected == null) {
selected = select();
}
throw new IllegalArgumentException("Can't find any suitable variants");
return selected.name;
}
static class Variant<T> {
@@ -57,17 +54,15 @@ public class NativeCodeLoader<T> {
this.supplier = supplier;
}
public boolean setup() {
private void setup() {
if (available && !hasBeenSetup) {
try {
setup.run();
hasBeenSetup = true;
} catch (Exception e) {
//logger.error("Unable to set up {}", name, e);
available = false;
}
}
return hasBeenSetup;
}
public T get() {
@@ -83,10 +78,9 @@ public class NativeCodeLoader<T> {
}
}
public static final BooleanSupplier MACOS = () -> System.getProperty("os.name").equalsIgnoreCase("Mac OS X") &&
static final BooleanSupplier MACOS = () -> System.getProperty("os.name").equalsIgnoreCase("Mac OS X") &&
System.getProperty("os.arch").equals("x86_64");
public static final BooleanSupplier LINUX = () -> System.getProperties().getProperty("os.name").equalsIgnoreCase("Linux") &&
static final BooleanSupplier LINUX = () -> System.getProperties().getProperty("os.name").equalsIgnoreCase("Linux") &&
System.getProperty("os.arch").equals("amd64");
public static final BooleanSupplier MAC_AND_LINUX = () -> MACOS.getAsBoolean() || LINUX.getAsBoolean();
public static final BooleanSupplier ALWAYS = () -> true;
static final BooleanSupplier ALWAYS = () -> true;
}