content: package com.airbnb.plog.handlers;
  
  import com.airbnb.plog.Message;
  import com.airbnb.plog.MessageImpl;
  import com.eclipsesource.json.JsonObject;
  import com.typesafe.config.Config;
  import io.netty.buffer.ByteBuf;
  import io.netty.channel.ChannelHandlerContext;
  import io.netty.channel.SimpleChannelInboundHandler;
  import lombok.RequiredArgsConstructor;
  
  @RequiredArgsConstructor
  public class TruncationProvider implements HandlerProvider {
      @Override
      public Handler getHandler(Config config) throws Exception {
          final int maxLength = config.getInt("max_length");
  
          return new MessageSimpleChannelInboundHandler(maxLength);
      }
  
      private static class MessageSimpleChannelInboundHandler extends SimpleChannelInboundHandler<Message> implements Handler {
          private final int maxLength;
  
          public MessageSimpleChannelInboundHandler(int maxLength) {
              super(false);
              this.maxLength = maxLength;
          }
  
          @Override
          protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
              final ByteBuf orig = msg.content();
              final int length = orig.readableBytes();
  
              if (length <= maxLength) {
                  ctx.fireChannelRead(msg);
              } else {
                  final ByteBuf content = msg.content().slice(0, maxLength);
                  ctx.fireChannelRead(new MessageImpl(content, msg.getTags()));
              }
          }
  
          @Override
          public JsonObject getStats() {
              return new JsonObject().add("max_length", maxLength);
          }
  
          @Override
          public String getName() {
              return "truncate";
          }
      }
  }
  