// Forward declaration of the read4 API.
int read4(char *buf);

class Solution
{
public:
    /**
     * @param buf Destination buffer
     * @param n   Number of characters to read
     * @return    The number of actual characters read
     */
    int read(char *buf, int n)
    {
        int total = 0;
        while (1)
        {
            int ret = read4(buf + total);
            total += ret;
            if (ret < 4 || total >= n)
                break;
        }
        if (total > n)
            total = n;
        buf[total] = '\0';
        return total;
    }
};