private static void writeFilter(BerBuffer buffer, byte[] filter, int filterEndIndex) {
        int currentFilterEndIndex;
        int balance;
        boolean escape;
        int currentParensIndex = 0;
        Context context = new Context();

        for (context.readIndex = 0; context.readIndex < filterEndIndex;) {
            switch (filter[context.readIndex]) {
                case '(' -> {
                    context.readIndex++;
                    currentParensIndex++;
                    switch (filter[context.readIndex]) {
                        case '&':
                            writeFilterSet(buffer, filter, TYPE_AND, context, filterEndIndex);
                            currentParensIndex--;
                            break;
                        case '|':
                            writeFilterSet(buffer, filter, TYPE_OR, context, filterEndIndex);
                            currentParensIndex--;
                            break;
                        case '!':
                            writeFilterSet(buffer, filter, TYPE_NOT, context, filterEndIndex);
                            currentParensIndex--;
                            break;
                        default:
                            balance = 1;
                            escape = false;
                            currentFilterEndIndex = context.readIndex;
                            while (currentFilterEndIndex < filterEndIndex && balance > 0) {
                                if (!escape) {
                                    if (filter[currentFilterEndIndex] == '(') {
                                        balance++;
                                    } else if (filter[currentFilterEndIndex] == ')') {
                                        balance--;
                                    }
                                }
                                escape = filter[currentFilterEndIndex] == '\\' && !escape;
                                if (balance > 0) {
                                    currentFilterEndIndex++;
                                }
                            }
                            if (balance != 0) {
                                throw new LdapException(
                                        ResultCode.FILTER_ERROR,
                                        "Unbalanced parenthesis");
                            }
                            writeFilter(buffer, filter, context.readIndex, currentFilterEndIndex);
                            context.readIndex = currentFilterEndIndex + 1;
                            currentParensIndex--;
                            break;
                    }
                }
                case ')' -> {
                    buffer.endSequence();
                    context.readIndex++;
                    currentParensIndex--;
                }
                case ' ' -> context.readIndex++;
                default -> {
                    writeFilter(buffer, filter, context.readIndex, filterEndIndex);
                    context.readIndex = filterEndIndex;
                }
            }
            if (currentParensIndex < 0) {
                throw new LdapException(ResultCode.FILTER_ERROR, "Unbalanced parenthesis");
            }
        }
        if (currentParensIndex != 0) {
            throw new LdapException(ResultCode.FILTER_ERROR, "Unbalanced parenthesis");
        }
    }
@author James Chen
@implSpec <a href="https://tools.ietf.org/html/rfc4515"> RFC 4515: String Representation of
          Search Filters</a>
turms-gateway/src/main/java/im/turms/gateway/infra/ldap/element/operation/search/Filter.java