Working on improvements to natives. Not done yet.
This commit is contained in:
@@ -9,9 +9,7 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
|
||||
|
||||
public static final VelocityCompressorFactory FACTORY = LibdeflateVelocityCompressor::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;
|
||||
|
||||
@@ -21,8 +19,8 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
|
||||
throw new IllegalArgumentException("Invalid compression level " + level);
|
||||
}
|
||||
|
||||
this.inflateCtx = inflate.init();
|
||||
this.deflateCtx = deflate.init(correctedLevel);
|
||||
this.inflateCtx = NativeZlibInflate.init();
|
||||
this.deflateCtx = NativeZlibDeflate.init(correctedLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,7 +38,7 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
|
||||
long sourceAddress = source.memoryAddress() + source.readerIndex();
|
||||
long destinationAddress = destination.memoryAddress() + destination.writerIndex();
|
||||
|
||||
inflate.process(inflateCtx, sourceAddress, source.readableBytes(), destinationAddress,
|
||||
NativeZlibInflate.process(inflateCtx, sourceAddress, source.readableBytes(), destinationAddress,
|
||||
uncompressedSize);
|
||||
destination.writerIndex(destination.writerIndex() + uncompressedSize);
|
||||
}
|
||||
@@ -53,7 +51,7 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
|
||||
long sourceAddress = source.memoryAddress() + source.readerIndex();
|
||||
long destinationAddress = destination.memoryAddress() + destination.writerIndex();
|
||||
|
||||
int produced = deflate.process(deflateCtx, sourceAddress, source.readableBytes(),
|
||||
int produced = NativeZlibDeflate.process(deflateCtx, sourceAddress, source.readableBytes(),
|
||||
destinationAddress, destination.writableBytes());
|
||||
if (produced > 0) {
|
||||
destination.writerIndex(destination.writerIndex() + produced);
|
||||
@@ -72,8 +70,8 @@ public class LibdeflateVelocityCompressor implements VelocityCompressor {
|
||||
@Override
|
||||
public void close() {
|
||||
if (!disposed) {
|
||||
inflate.free(inflateCtx);
|
||||
deflate.free(deflateCtx);
|
||||
NativeZlibInflate.free(inflateCtx);
|
||||
NativeZlibDeflate.free(deflateCtx);
|
||||
}
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ package com.velocitypowered.natives.compression;
|
||||
*/
|
||||
class NativeZlibDeflate {
|
||||
|
||||
native long init(int level);
|
||||
static native long init(int level);
|
||||
|
||||
native long free(long ctx);
|
||||
static native long free(long ctx);
|
||||
|
||||
native int process(long ctx, long sourceAddress, int sourceLength, long destinationAddress,
|
||||
static native int process(long ctx, long sourceAddress, int sourceLength, long destinationAddress,
|
||||
int destinationLength);
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@ import java.util.zip.DataFormatException;
|
||||
*/
|
||||
class NativeZlibInflate {
|
||||
|
||||
native long init();
|
||||
static native long init();
|
||||
|
||||
native long free(long ctx);
|
||||
static native long free(long ctx);
|
||||
|
||||
native boolean process(long ctx, long sourceAddress, int sourceLength, long destinationAddress,
|
||||
int destinationLength) throws DataFormatException;
|
||||
static native boolean process(long ctx, long sourceAddress, int sourceLength,
|
||||
long destinationAddress, int destinationLength) throws DataFormatException;
|
||||
}
|
||||
|
||||
@@ -19,13 +19,11 @@ public class NativeVelocityCipher implements VelocityCipher {
|
||||
return new NativeVelocityCipher(false, key);
|
||||
}
|
||||
};
|
||||
private static final OpenSslCipherImpl impl = new OpenSslCipherImpl();
|
||||
|
||||
private final long ctx;
|
||||
private boolean disposed = false;
|
||||
|
||||
private NativeVelocityCipher(boolean encrypt, SecretKey key) throws GeneralSecurityException {
|
||||
this.ctx = impl.init(key.getEncoded(), encrypt);
|
||||
this.ctx = OpenSslCipherImpl.init(key.getEncoded(), encrypt);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -35,13 +33,13 @@ public class NativeVelocityCipher implements VelocityCipher {
|
||||
long base = source.memoryAddress() + source.readerIndex();
|
||||
int len = source.readableBytes();
|
||||
|
||||
impl.process(ctx, base, len, base);
|
||||
OpenSslCipherImpl.process(ctx, base, len, base);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (!disposed) {
|
||||
impl.free(ctx);
|
||||
OpenSslCipherImpl.free(ctx);
|
||||
}
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import java.security.GeneralSecurityException;
|
||||
|
||||
class OpenSslCipherImpl {
|
||||
|
||||
native long init(byte[] key, boolean encrypt) throws GeneralSecurityException;
|
||||
static native long init(byte[] key, boolean encrypt) throws GeneralSecurityException;
|
||||
|
||||
native void process(long ctx, long source, int len, long dest);
|
||||
static native void process(long ctx, long source, int len, long dest);
|
||||
|
||||
native void free(long ptr);
|
||||
static native void free(long ptr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user