Skip to content

Commit 5d90de5

Browse files
author
jython234
committed
Reduced size of ByteBuffers to reduce memory use.
1 parent 2c63430 commit 5d90de5

4 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/main/java/net/beaconpe/jraklib/protocol/EncapsulatedPacket.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class EncapsulatedPacket {
4343
public boolean needACK = false;
4444
public int identifierACK = -1;
4545

46+
public int bufferLength;
47+
4648
protected int offset;
4749

4850
public static EncapsulatedPacket fromBinary(byte[] binary){
@@ -55,6 +57,7 @@ public static EncapsulatedPacket fromBinary(byte[] binary, boolean internal){
5557

5658
public static EncapsulatedPacket fromBinary(byte[] binary, boolean internal, int offset){
5759
EncapsulatedPacket packet = new EncapsulatedPacket();
60+
packet.bufferLength = binary.length;
5861
byte flags = binary[0];
5962
packet.reliability = (byte) ((flags & 0b11100000) >> 5);
6063
packet.hasSplit = (flags & 0b00010000) > 0;
@@ -111,7 +114,7 @@ public int getTotalLength(boolean internal){
111114

112115
public byte[] toBinary(boolean internal){
113116
int offset = 0;
114-
ByteBuffer bb = ByteBuffer.allocate(1024 * 1024 * 32);
117+
ByteBuffer bb = ByteBuffer.allocate(64 * 64 * 64);
115118
bb.put((byte) ((byte) (reliability << 5) | (hasSplit ? 0b00010000 : 0)));
116119
if(internal){
117120
bb.put(Binary.writeInt(buffer.length));
@@ -138,6 +141,7 @@ public byte[] toBinary(boolean internal){
138141

139142
bb.put(buffer);
140143
byte[] data = Arrays.copyOf(bb.array(), bb.position());
144+
bufferLength = data.length;
141145
bb = null;
142146
return data;
143147
}

src/main/java/net/beaconpe/jraklib/protocol/Packet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ protected void putAddress(String addr, int port, byte version){
181181
protected abstract void _decode();
182182

183183
public void encode(){
184-
sendBuffer = ByteBuffer.allocate(1024*1024);
184+
sendBuffer = ByteBuffer.allocate(64 * 64 * 64);
185185
putByte(getID());
186186
_encode();
187187
buffer = ArrayUtils.subarray(sendBuffer.array(), 0, sendBuffer.position());

src/main/java/net/beaconpe/jraklib/server/ServerHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void sendEncapsulated(String identifier, EncapsulatedPacket packet){
4646
}
4747

4848
public void sendEncapsulated(String identifier, EncapsulatedPacket packet, byte flags){
49-
ByteBuffer bb = ByteBuffer.allocate(1024*1024*2);
49+
ByteBuffer bb = ByteBuffer.allocate(packet.bufferLength);
5050
bb.put(JRakLib.PACKET_ENCAPSULATED).put((byte) identifier.getBytes().length).put(identifier.getBytes()).put(flags).put(packet.toBinary(true));
5151
server.pushMainToThreadPacket(Arrays.copyOf(bb.array(), bb.position()));
5252
bb = null;

src/main/java/net/beaconpe/jraklib/server/Session.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private void handleSplit(EncapsulatedPacket packet) throws IOException {
330330

331331
if(splitPackets.get(packet.splitID).values().size() == packet.splitCount){
332332
EncapsulatedPacket pk = new EncapsulatedPacket();
333-
ByteBuffer bb = ByteBuffer.allocate(1024*1024);
333+
ByteBuffer bb = ByteBuffer.allocate(64 * 64 * 64);
334334
for(int i = 0; i < packet.splitCount; i++){
335335
bb.put(splitPackets.get(packet.splitID).get(i).buffer);
336336
}

0 commit comments

Comments
 (0)