All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickhouse.data.stream.IterableObjectInputStream Maven / Gradle / Ivy

There is a newer version: 0.7.1-patch1
Show newest version
package com.clickhouse.data.stream;

import java.io.IOException;
import java.util.Iterator;
import java.util.function.Function;

import com.clickhouse.data.ClickHouseByteBuffer;
import com.clickhouse.data.ClickHouseChecker;

public class IterableObjectInputStream extends AbstractByteArrayInputStream {
    private final Function func;
    private final Iterator it;

    public IterableObjectInputStream(Iterable source, Function converter, Runnable postCloseAction) {
        super(null, null, postCloseAction);

        func = ClickHouseChecker.nonNull(converter, "Converter");
        it = ClickHouseChecker.nonNull(source, "Source").iterator();
    }

    @Override
    protected int updateBuffer() throws IOException {
        position = 0;
        while (it.hasNext()) {
            T obj = it.next();
            byte[] bytes = obj != null ? func.apply(obj) : null;
            if (bytes != null && bytes.length > 0) {
                buffer = bytes;
                if (copyTo != null) {
                    copyTo.write(bytes);
                }
                return limit = bytes.length;
            }
        }
        buffer = ClickHouseByteBuffer.EMPTY_BYTES;
        return limit = 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy