package org.implab.gradle.common.exec;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CompletableFuture;

public interface RedirectFrom {
    CompletableFuture<Void> redirect(OutputStream to);

    public static RedirectFrom file(final File file) {
        return to -> CompletableFuture.runAsync(() -> {
            try (var from = new FileInputStream(file); to) {
                from.transferTo(to);
            } catch (Exception e) {
                // silence!
            }
        });
    }

    public static RedirectFrom stream(final InputStream from) {
        return to -> CompletableFuture.runAsync(() -> {
            try (from; to) {
                from.transferTo(to);
            } catch (Exception e) {
                // silence!
            }
        });
    }


}
