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

org.qbicc.machine.tool.process.OutputStreamSupplierOutputDestination 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.function.ExceptionFunction;

final class OutputStreamSupplierOutputDestination extends OutputDestination {
    private final ExceptionFunction function;
    private final T param;

    OutputStreamSupplierOutputDestination(final ExceptionFunction function, final T param) {
        this.function = function;
        this.param = param;
    }

    OutputStream openStream() throws IOException {
        return function.apply(param);
    }

    void transferFrom(final InputSource source) throws IOException {
        // some sources have more optimal ways to write to an output stream
        try (OutputStream os = openStream()) {
            source.transferTo(os);
        }
    }

    void transferFrom(final InputStream stream) throws IOException {
        try (OutputStream os = openStream()) {
            stream.transferTo(os);
        }
    }

    void transferFrom(final Reader reader, final Charset charset) throws IOException {
        try (OutputStream os = openStream()) {
            try (OutputStreamWriter osw = new OutputStreamWriter(os, charset)) {
                reader.transferTo(osw);
            }
        }
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy