mirror of https://github.com/Minestom/Minestom.git
Prevent buffer being resized in `writeFramedPacket`, it is now possible to know if a buffer couldn't be written to
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
f023b1bc07
commit
d7a116c144
|
@ -193,7 +193,7 @@ public final class PacketUtils {
|
|||
final int contentStart = buffer.position();
|
||||
|
||||
Utils.writeVarInt(buffer, packet.getId());
|
||||
packet.write(new BinaryWriter(buffer));
|
||||
packet.write(BinaryWriter.view(buffer)); // ensure that the buffer is not resized/changed
|
||||
final int packetSize = buffer.position() - contentStart;
|
||||
if (packetSize >= MinecraftServer.getCompressionThreshold()) {
|
||||
// Packet large enough, compress
|
||||
|
@ -207,11 +207,11 @@ public final class PacketUtils {
|
|||
deflater.deflate(buffer);
|
||||
deflater.reset();
|
||||
|
||||
Utils.writeVarIntHeader(buffer, compressedIndex, (buffer.position() - contentStart) + 3);
|
||||
Utils.writeVarIntHeader(buffer, uncompressedIndex, packetSize);
|
||||
Utils.writeVarIntHeader(buffer, compressedIndex, buffer.position() - uncompressedIndex);
|
||||
Utils.writeVarIntHeader(buffer, uncompressedIndex, packetSize); // Data Length
|
||||
} else {
|
||||
Utils.writeVarIntHeader(buffer, compressedIndex, packetSize + 3);
|
||||
Utils.writeVarIntHeader(buffer, uncompressedIndex, 0);
|
||||
Utils.writeVarIntHeader(buffer, uncompressedIndex, 0); // Data Length (0 since uncompressed)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -246,7 +246,7 @@ public final class PacketUtils {
|
|||
}
|
||||
|
||||
public static LocalCache get(String name, int size) {
|
||||
return CACHES.computeIfAbsent(name, s -> new LocalCache(name, size));
|
||||
return CACHES.computeIfAbsent(name, s -> new LocalCache(s, size));
|
||||
}
|
||||
|
||||
public String name() {
|
||||
|
|
|
@ -7,6 +7,7 @@ import net.minestom.server.coordinate.Point;
|
|||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.utils.SerializerUtils;
|
||||
import net.minestom.server.utils.Utils;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTWriter;
|
||||
|
@ -29,32 +30,33 @@ public class BinaryWriter extends OutputStream {
|
|||
private ByteBuffer buffer;
|
||||
private NBTWriter nbtWriter; // Lazily initialized
|
||||
|
||||
/**
|
||||
* Creates a {@link BinaryWriter} using a heap buffer with a custom initial capacity.
|
||||
*
|
||||
* @param initialCapacity the initial capacity of the binary writer
|
||||
*/
|
||||
public BinaryWriter(int initialCapacity) {
|
||||
this.buffer = ByteBuffer.allocate(initialCapacity);
|
||||
private final boolean resizable;
|
||||
|
||||
private BinaryWriter(ByteBuffer buffer, boolean resizable) {
|
||||
this.buffer = buffer;
|
||||
this.resizable = resizable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link BinaryWriter} from multiple a single buffer.
|
||||
*
|
||||
* @param buffer the writer buffer
|
||||
*/
|
||||
public BinaryWriter(@NotNull ByteBuffer buffer) {
|
||||
this.buffer = buffer;
|
||||
this.resizable = true;
|
||||
}
|
||||
|
||||
public BinaryWriter(int initialCapacity) {
|
||||
this(ByteBuffer.allocate(initialCapacity));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link BinaryWriter} with a "reasonably small initial capacity".
|
||||
*/
|
||||
public BinaryWriter() {
|
||||
this(255);
|
||||
}
|
||||
|
||||
@ApiStatus.Experimental
|
||||
public static BinaryWriter view(ByteBuffer buffer) {
|
||||
return new BinaryWriter(buffer, false);
|
||||
}
|
||||
|
||||
protected void ensureSize(int length) {
|
||||
if (!resizable) return;
|
||||
final int position = buffer.position();
|
||||
if (position + length >= buffer.limit()) {
|
||||
final int newLength = (position + length) * 4;
|
||||
|
|
Loading…
Reference in New Issue