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

com.clickhouse.data.stream.IterableByteBufferInputStream 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.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Iterator;

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

public class IterableByteBufferInputStream extends AbstractByteBufferInputStream {
    private final Iterator it;

    public IterableByteBufferInputStream(Iterable source, Runnable postCloseAction) {
        super(null, null, postCloseAction);

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

    @Override
    protected int updateBuffer() throws IOException {
        while (it.hasNext()) {
            ByteBuffer bytes = it.next();
            if (bytes != null && bytes.hasRemaining()) {
                buffer = bytes;
                if (copyTo != null) {
                    int position = bytes.position();
                    if (bytes.hasArray()) {
                        copyTo.write(bytes.array(), position, bytes.remaining());
                    } else {
                        byte[] b = new byte[bytes.remaining()];
                        bytes.get(b);
                        copyTo.write(b);
                        ((Buffer) bytes).position(position);
                    }
                }
                return bytes.remaining();
            }
        }
        buffer = ClickHouseByteBuffer.EMPTY_BUFFER;
        return 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy