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

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

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

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;

import io.smallrye.common.function.ExceptionFunction;

final class AppendableSupplierOutputDestination extends OutputDestination {
    private final ExceptionFunction supplier;
    private final T param;
    private final Charset charset;

    AppendableSupplierOutputDestination(final ExceptionFunction supplier, final T param, final Charset charset) {
        this.supplier = supplier;
        this.param = param;
        this.charset = charset;
    }

    void transferFrom(final InputStream is) throws IOException {
        try (InputStreamReader r = new InputStreamReader(is, charset)) {
            transferFrom(r, charset);
        }
    }

    void transferFrom(final Reader reader, final Charset charset) throws IOException {
        Appendable destination = openAppendable();
        if (destination instanceof Closeable) {
            try (Closeable closeable = (Closeable) destination) {
                if (destination instanceof Writer) {
                    reader.transferTo((Writer) destination);
                } else {
                    reader.transferTo(new AppendableWriter(destination));
                }
            }
        } else {
            reader.transferTo(new AppendableWriter(destination));
        }
    }

    Appendable openAppendable() throws IOException {
        return supplier.apply(param);
    }

    void transferFrom(final EmptyInputSource source) {
        // optimization
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy