private int binarySearch(ByteBuffer idx, long target, IndexSearchType searchEntity,
                             SearchResultType searchResultType, int begin, int end) {
        // binary search for the entry
        int lo = begin;
        int hi = end;
        while (lo < hi) {
            int mid = (lo + hi + 1) >>> 1;
            IndexEntry found = parseEntry(idx, mid);
            int compareResult = compareIndexEntry(found, target, searchEntity);
            if (compareResult > 0)
                hi = mid - 1;
            else if (compareResult < 0)
                lo = mid;
            else
                return mid;
        }
        switch (searchResultType) {
            case LARGEST_LOWER_BOUND:
                return lo;
            case SMALLEST_UPPER_BOUND:
                if (lo == entries - 1)
                    return -1;
                else
                    return lo + 1;
            default:
                throw new IllegalStateException("Unexpected searchResultType " + searchResultType);
        }
    }
Lookup lower or upper bounds for the given target.
storage/src/main/java/org/apache/kafka/storage/internals/log/AbstractIndex.java