@Override
    public int read(byte[] buffer, int offset, int len) throws IOException {
        if (offset < 0 || len < 0 || len > buffer.length - offset) {
            throw new IndexOutOfBoundsException("Invalid bounds");
        }
        if (len == 0) {
            return 0;
        }
        int totalRead = 0;
        int read;
        do {
            read = super.read(buffer, offset + totalRead, len - totalRead - offset);
            if (read == -1) {
                break;
            }
            totalRead += read;
        } while (offset + totalRead < len);

        return totalRead == 0 ? -1 : totalRead;
    }
Similar to a {@code readLine} method, but matches what OpenSSL expects
from a {@code BIO_gets} method.
common/src/main/java/org/conscrypt/OpenSSLBIOInputStream.java