protected long onWrite(ClickHouseConfig config, SocketChannel sc, ClickHouseInputStream in, long startPosition)
            throws IOException {
        final long socketTimeout = config.getSocketTimeout();
        final long startTime = socketTimeout > 0L ? System.currentTimeMillis() : 0L;

        final ClickHousePassThruStream s = in.getUnderlyingStream();
        if (s.hasInput() && s instanceof ClickHouseFile) {
            try (FileChannel fc = FileChannel.open(((ClickHouseFile) s).getFile().toPath())) {
                long size = fc.size();
                long chunkSize = config.getRequestChunkSize();
                long offset = startPosition;
                while (size > 0) {
                    // TODO write chunk header
                    long transferred = fc.transferTo(offset, size >= chunkSize ? chunkSize : size, sc);
                    if (transferred == 0) { // output buffer is jammed
                        return offset;
                    }
                    size -= transferred;
                    offset += transferred;
                }
            }
        } else {
            ByteBuffer buffer = ByteBuffer.allocate(config.getReadBufferSize());
            byte[] bytes = buffer.array();
            int len = 0;
            while ((len = in.read(bytes)) > 0) {
                buffer.limit(len);
                log.trace("Send to [%s]: [%s]", in, new String(bytes, 0, len));
                while (buffer.hasRemaining()) {
                    if (sc.write(buffer) == 0) {
                        return 1L;
                    }
                    if (startTime > 0L && System.currentTimeMillis() - startTime > socketTimeout) {
                        throw new SocketTimeoutException(ClickHouseUtils.format(ERROR_WRITE_TIMEOUT, socketTimeout));
                    }
                }
            }
        }

        return 0L;
    }
Reads byte from socket input buffer.

@param config non-null configuration
@param sc     socket channel
@param out    output stream
@return true if the read was a success; false otherwise
@throws IOException
clickhouse-client/src/main/java/com/clickhouse/client/AbstractSocketClient.java