package com.airbnb.plog;

import io.netty.buffer.ByteBuf;
import io.netty.channel.socket.DatagramPacket;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.nio.ByteOrder;

@ToString
@RequiredArgsConstructor
public class MultiPartMessageFragment {
    @Getter
    private final int fragmentCount;
    @Getter
    private final int fragmentIndex;
    @Getter
    private final int fragmentSize;
    @Getter
    private final long msgId;
    @Getter
    private final int totalLength;
    @Getter
    private final ByteBuf payload;

    static MultiPartMessageFragment fromDatagram(DatagramPacket packet) {
        final ByteBuf content = packet.content().order(ByteOrder.BIG_ENDIAN);
        final int length = content.readableBytes();
        if (length < 24)
            throw new IllegalArgumentException("Packet too short: " + length + " bytes");

        final int fragmentCount = content.getUnsignedShort(2);
        final int fragmentIndex = content.getUnsignedShort(4);
        if (fragmentIndex > fragmentCount)
            throw new IllegalArgumentException("Index " + fragmentIndex + " < count " + fragmentCount);
        final int packetSize = content.getUnsignedShort(6);
        final int idRightPart = content.getInt(8);
        final int totalLength = content.getInt(12);
        final ByteBuf payload = content.slice(24, packetSize);

        final int port = packet.sender().getPort();
        return new MultiPartMessageFragment(
                fragmentCount, fragmentIndex, packetSize,
                (port << 32) + idRightPart,
                totalLength, payload);
    }

    boolean isAlone() {
        return fragmentCount == 0;
    }
}

@@ -287,16 +287,56 @@
 gment %7B%0A
+    static final int HEADER_SIZE = 24;%0A%0A
     @Get
@@ -331,32 +331,32 @@
 4;%0A%0A    @Getter%0A
-
     private fina
@@ -835,18 +835,27 @@
 ength %3C 
-24
+HEADER_SIZE
 )%0A      
@@ -1243,16 +1243,25 @@
 tSize = 
+Math.min(
 content.
@@ -1279,16 +1279,56 @@
 Short(6)
+, content.readableBytes() - HEADER_SIZE)
 ;%0A      
@@ -1470,18 +1470,27 @@
 t.slice(
-24
+HEADER_SIZE
 , packet

fix for small packets