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

org.qbicc.machine.tool.process.ReaderSupplierInputSource Maven / Gradle / Ivy

There is a newer version: 0.77.0
Show newest version
package org.qbicc.machine.tool.process;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.charset.Charset;

import io.smallrye.common.constraint.Assert;
import io.smallrye.common.function.ExceptionFunction;

final class ReaderSupplierInputSource extends InputSource {
    private final ExceptionFunction readerFactory;
    private final T param;
    private final Charset charset;

    ReaderSupplierInputSource(final ExceptionFunction readerFactory, final T param, final Charset charset) {
        this.readerFactory = readerFactory;
        this.param = param;
        this.charset = charset;
    }

    public void transferTo(final OutputDestination destination) throws IOException {
        try (Reader reader = openReader()) {
            destination.transferFrom(reader, charset);
        }
    }

    ProcessBuilder.Redirect getInputRedirect() {
        return ProcessBuilder.Redirect.PIPE;
    }

    void transferTo(final OutputStream os) throws IOException {
        try (Reader reader = openReader()) {
            try (OutputStreamWriter writer = new OutputStreamWriter(os, charset)) {
                reader.transferTo(writer);
            }
        }
    }

    Reader openReader() throws IOException {
        return readerFactory.apply(param);
    }

    InputStream openStream() {
        throw Assert.unsupported();
    }

    @Override
    public String toString() {
        return "";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy