@Override
    public byte[] readBytes(int length) throws IOException {
        if (length < 1) {
            return ClickHouseByteBuffer.EMPTY_BYTES;
        }
        ensureOpen();

        byte[] bytes = new byte[length];
        int offset = 0;
        ByteBuffer b = buffer;
        int remain = b.remaining();
        while (length > 0) {
            if (b == ClickHouseByteBuffer.EMPTY_BUFFER) {
                closeQuietly();
                throw offset == 0 ? new EOFException()
                        : new StreamCorruptedException(
                                ClickHouseUtils.format(ERROR_INCOMPLETE_READ, offset, bytes.length));
            }

            if (remain >= length) {
                b.get(bytes, offset, length);
                offset += length;
                length = 0;
            } else {
                b.get(bytes, offset, remain);
                offset += remain;
                length -= remain;
                remain = updateBuffer();
                b = buffer;
            }
        }

        return bytes;
    }
Update inner buffer as needed.

@return remaining bytes in buffer
@throws IOException when failed to update buffer
clickhouse-data/src/main/java/com/clickhouse/data/stream/AbstractByteBufferInputStream.java