@Nonnull
    @Override
    public Result addChunk(ByteBuf buffer) {
        final byte[] readable = new byte[buffer.readableBytes()];
        buffer.readBytes(readable, buffer.readerIndex(), buffer.readableBytes());

        final GELFMessage msg = new GELFMessage(readable);

        final ByteBuf aggregatedBuffer;
        switch (msg.getGELFType()) {
            case CHUNKED:
                try {
                    chunkCounter.inc();
                    aggregatedBuffer = checkForCompletion(msg);
                    if (aggregatedBuffer == null) {
                        return VALID_EMPTY_RESULT;
                    }
                } catch (IllegalArgumentException | IllegalStateException | IndexOutOfBoundsException e) {
                    log.debug("Invalid gelf message chunk, dropping message.", e);
                    return INVALID_RESULT;
                }
                break;
            case ZLIB:
            case GZIP:
            case UNCOMPRESSED:
                aggregatedBuffer = Unpooled.wrappedBuffer(readable);
                break;
            case UNSUPPORTED:
                return INVALID_RESULT;
            default:
                return INVALID_RESULT;
        }
        return new Result(aggregatedBuffer, true);
    }
@Test
    public void tooManyChunks() {
        final ByteBuf[] chunks = createChunkedMessage(129 * 1024, 1024);
        int i = 1;
        for (final ByteBuf chunk : chunks) {
            final CodecAggregator.Result result = aggregator.addChunk(chunk);
            if (i == 129) {
                assertFalse("Message invalidated (chunk #" + i + ")", result.isValid());
                assertNull("Message discarded (chunk #" + i + ")", result.getMessage());

            } else {
                assertTrue("Incomplete message valid (chunk #" + i + ")", result.isValid());
                assertNull("Message not complete (chunk #" + i + ")", result.getMessage());
            }
            i++;
        }
    }