#3737: Use composite buffers where possible

This commit is contained in:
Outfluencer
2024-09-09 21:01:19 +10:00
committed by md_5
parent 477ea5983c
commit b309e4ac50
12 changed files with 123 additions and 23 deletions

View File

@@ -18,4 +18,10 @@ public interface BungeeCipher
void cipher(ByteBuf in, ByteBuf out) throws GeneralSecurityException;
ByteBuf cipher(ChannelHandlerContext ctx, ByteBuf in) throws GeneralSecurityException;
/*
* This indicates whether the input ByteBuf is allowed to be a CompositeByteBuf.
* If you need access to a memory address, you should not allow composite buffers.
*/
boolean allowComposite();
}

View File

@@ -2,6 +2,7 @@ package net.md_5.bungee.jni.cipher;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.FastThreadLocal;
import java.security.GeneralSecurityException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
@@ -12,10 +13,10 @@ public class JavaCipher implements BungeeCipher
{
private final Cipher cipher;
private static final ThreadLocal<byte[]> heapInLocal = new EmptyByteThreadLocal();
private static final ThreadLocal<byte[]> heapOutLocal = new EmptyByteThreadLocal();
private static final FastThreadLocal<byte[]> heapInLocal = new EmptyByteThreadLocal();
private static final FastThreadLocal<byte[]> heapOutLocal = new EmptyByteThreadLocal();
private static class EmptyByteThreadLocal extends ThreadLocal<byte[]>
private static class EmptyByteThreadLocal extends FastThreadLocal<byte[]>
{
@Override
@@ -88,4 +89,10 @@ public class JavaCipher implements BungeeCipher
in.readBytes( heapIn, 0, readableBytes );
return heapIn;
}
@Override
public boolean allowComposite()
{
return true;
}
}

View File

@@ -71,4 +71,10 @@ public class NativeCipher implements BungeeCipher
return heapOut;
}
@Override
public boolean allowComposite()
{
return false;
}
}

View File

@@ -6,9 +6,17 @@ import java.util.zip.DataFormatException;
public interface BungeeZlib
{
public static final int OUTPUT_BUFFER_SIZE = 8192;
void init(boolean compress, int level);
void free();
void process(ByteBuf in, ByteBuf out) throws DataFormatException;
/*
* This indicates whether the input ByteBuf is allowed to be a CompositeByteBuf.
* If you need access to a memory address, you should not allow composite buffers.
*/
boolean allowComposite();
}

View File

@@ -73,4 +73,10 @@ public class JavaZlib implements BungeeZlib
inflater.reset();
}
}
@Override
public boolean allowComposite()
{
return true;
}
}

View File

@@ -55,7 +55,7 @@ public class NativeZlib implements BungeeZlib
while ( !nativeCompress.finished && ( compress || in.isReadable() ) )
{
out.ensureWritable( 8192 );
out.ensureWritable( OUTPUT_BUFFER_SIZE );
int processed;
try
@@ -74,4 +74,10 @@ public class NativeZlib implements BungeeZlib
nativeCompress.consumed = 0;
nativeCompress.finished = false;
}
@Override
public boolean allowComposite()
{
return false;
}
}