@Override
    public ClickHouseByteBuffer readBufferUntil(byte[] separator) throws IOException {
        if (separator == null || separator.length < 1) {
            return byteBuffer.reset();
        }
        ensureOpen();

        int slen = separator.length;
        LinkedList<byte[]> list = new LinkedList<>();
        int length = 0;
        boolean more = true;
        while (more) {
            int remain = buffer.remaining();
            if (remain < 1) {
                closeQuietly();
                more = false;
            } else {
                int position = buffer.position();
                int limit = buffer.limit();
                byte[] bytes = ClickHouseByteUtils.getOrCopy(buffer, remain);
                int read = ClickHouseByteUtils.indexOf(bytes, 0, remain, separator, 0, slen, true);
                int missed = 0;
                if (read == -1 || (missed = slen + read - limit) > 0) {
                    while (true) {
                        list.add(bytes);
                        length += remain;
                        ((Buffer) buffer).position(limit);
                        if ((remain = updateBuffer()) < 1) {
                            closeQuietly();
                            more = false;
                            break;
                        }

                        if (missed > 0) {
                            bytes = ClickHouseByteUtils.getOrCopy(buffer, remain);
                            if (remain < missed) {
                                if (ClickHouseByteUtils.equals(bytes, 0, missed, separator, slen - missed,
                                        slen - missed + missed)) {
                                    missed -= remain;
                                } else {
                                    missed = 0;
                                }
                            } else {
                                if (ClickHouseByteUtils.equals(bytes, 0, missed, separator, slen - missed,
                                        slen)) {
                                    length += missed;
                                    ((Buffer) buffer).position(missed);
                                    list.add(Arrays.copyOfRange(bytes, 0, missed));
                                    more = false;
                                }
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                } else {
                    read += slen;
                    list.add(bytes);
                    length += read;
                    ((Buffer) buffer).position(position + read);
                    more = false;
                }
            }
        }
        return byteBuffer.update(list, 0, length);
    }
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