public static void maybeCheckMessageLength(SerializationFormat serializationFormat, ByteBuf buf,
                                               int maxStringLength) throws TProtocolException {
        if (supportsMessageLengthLimit(serializationFormat) || maxStringLength <= 0) {
            return;
        }

        if (buf.readableBytes() < 4) {
            // Delegate the malformed message to readMessageBegin() to handle it.
            return;
        }

        final int length = buf.getInt(buf.readerIndex());
        if (length < 0) {
            // A negative value means tbinary protocol V1 for which TBinaryProtocol correctly validates
            // the message length before creating an array.
            // https://github.com/apache/thrift/blob/0.9.3/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java#L225
            // https://github.com/apache/thrift/blob/0.9.3/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java#L358
            return;
        }

        if (length > maxStringLength) {
            // Follow the error format of the upstream so that users get the same error regardless of
            // Thrift versions.
            // https://github.com/apache/thrift/blob/5b158389b01d028e98e59f0ea41c01d625a84242/lib/java/src/main/java/org/apache/thrift/protocol/TBinaryProtocol.java#L442
            throw new TProtocolException(TProtocolException.SIZE_LIMIT,
                                         "Length exceeded max allowed: " + length);
        }
    }
Optionally validates the length a Thrift's message if the {@link SerializationFormat} is
{@link ThriftSerializationFormats#BINARY} and the Thrift runtime does not validate the length for
`readMessageBegin()`.
thrift/thrift0.13/src/main/java/com/linecorp/armeria/internal/common/thrift/ThriftProtocolUtil.java