package org.ghidra.bridge.runtime;

import java.io.OutputStream;
import java.io.PrintStream;

public final class OutputSilencer {
    private OutputSilencer() {
    }

    public static <T> T suppress(ThrowingOperation<T> operation) throws Exception {
        PrintStream originalOut = System.out;
        PrintStream originalErr = System.err;
        try (
            PrintStream nullOut = new PrintStream(OutputStream.nullOutputStream());
            PrintStream nullErr = new PrintStream(OutputStream.nullOutputStream())
        ) {
            System.setOut(nullOut);
            System.setErr(nullErr);
            return operation.run();
        } finally {
            System.setOut(originalOut);
            System.setErr(originalErr);
        }
    }

    public interface ThrowingOperation<T> {
        T run() throws Exception;
    }
}
