<commit_before>/*
 * Autopsy Forensic Browser
 * 
 * Copyright 2013 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.coreutils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.logging.Level;
import org.sleuthkit.autopsy.coreutils.Logger;

/**
 * Takes care of forking a process and reading output / error streams to either a
 * string buffer or directly to a file writer
 */
public final class ExecUtil {

    private static final Logger logger = Logger.getLogger(ExecUtil.class.getName());
    private Process proc = null;
    private String command = null;
    private ExecUtil.StreamToStringRedirect errorStringRedirect = null;
    private ExecUtil.StreamToStringRedirect outputStringRedirect = null;
    private ExecUtil.StreamToWriterRedirect outputWriterRedirect = null;

    /**
     * Execute a process. Redirect asynchronously stdout to a string and stderr
     * to nowhere. Use only for small outputs, otherwise use the execute()
     * variant with Writer.
     *
     * @param aCommand command to be executed
     * @param params parameters of the command
     * @return string buffer with captured stdout
     */
    public synchronized String execute(final String aCommand, final String... params) throws IOException, InterruptedException {
        String output = "";

        // build command array
        String[] arrayCommand = new String[params.length + 1];
        arrayCommand[0] = aCommand;

        StringBuilder arrayCommandToLog = new StringBuilder();
        arrayCommandToLog.append(aCommand).append(" ");

        for (int i = 1; i < arrayCommand.length; i++) {
            arrayCommand[i] = params[i - 1];
            arrayCommandToLog.append(arrayCommand[i]).append(" ");
        }

        final Runtime rt = Runtime.getRuntime();
        logger.log(Level.INFO, "Executing " + arrayCommandToLog.toString());

        proc = rt.exec(arrayCommand);

        //stderr redirect
        errorStringRedirect = new ExecUtil.StreamToStringRedirect(proc.getErrorStream(), "ERROR");
        //stdout redirect
        outputStringRedirect = new ExecUtil.StreamToStringRedirect(proc.getInputStream(), "OUTPUT");

        //start redurectors
        errorStringRedirect.start();
        outputStringRedirect.start();

        //wait for process to complete and capture error core
        final int exitVal = proc.waitFor();
        logger.log(Level.INFO, aCommand + " exit value: " + exitVal);

        errorStringRedirect.stopRun();
        errorStringRedirect = null;

        outputStringRedirect.stopRun();
        output = outputStringRedirect.getOutput();

        outputStringRedirect = null;

        //gc process with its streams
        proc = null;

        return output;
    }

    /**
     * Execute a process. Redirect asynchronously stdout to a passed in writer
     * and stderr to nowhere.
     *
     * @param stdoutWriter file writer to write stdout to
     * @param aCommand command to be executed
     * @param params parameters of the command
     * @return string buffer with captured stdout
     */
    public synchronized void execute(final Writer stdoutWriter, final String aCommand, final String... params) throws IOException, InterruptedException {

        // build command array
        String[] arrayCommand = new String[params.length + 1];
        arrayCommand[0] = aCommand;

        StringBuilder arrayCommandToLog = new StringBuilder();
        arrayCommandToLog.append(aCommand).append(" ");

        for (int i = 1; i < arrayCommand.length; i++) {
            arrayCommand[i] = params[i - 1];
            arrayCommandToLog.append(arrayCommand[i]).append(" ");
        }

        final Runtime rt = Runtime.getRuntime();
        logger.log(Level.INFO, "Executing " + arrayCommandToLog.toString());

        proc = rt.exec(arrayCommand);

        //stderr redirect
        errorStringRedirect = new ExecUtil.StreamToStringRedirect(proc.getErrorStream(), "ERROR");
        //stdout redirect
        outputWriterRedirect = new ExecUtil.StreamToWriterRedirect(proc.getInputStream(), stdoutWriter);

        //start redurectors
        errorStringRedirect.start();
        outputWriterRedirect.start();

        //wait for process to complete and capture error core
        final int exitVal = proc.waitFor();
        logger.log(Level.INFO, aCommand + " exit value: " + exitVal);

        //gc process with its streams
        proc = null;
    }

    /**
     * Interrupt the running process and stop its stream redirect threads
     */
    public synchronized void stop() {
        logger.log(Level.INFO, "Stopping Execution of: " + command);

        if (errorStringRedirect != null) {
            errorStringRedirect.stopRun();<commit_msg>Not nullifying util processes - presumed fix for javaIO flushing error.<commit_after>/*
 * Autopsy Forensic Browser
 * 
 * Copyright 2013 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.coreutils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.logging.Level;
import org.sleuthkit.autopsy.coreutils.Logger;

/**
 * Takes care of forking a process and reading output / error streams to either a
 * string buffer or directly to a file writer
 */
public final class ExecUtil {

    private static final Logger logger = Logger.getLogger(ExecUtil.class.getName());
    private Process proc = null;
    private String command = null;
    private ExecUtil.StreamToStringRedirect errorStringRedirect = null;
    private ExecUtil.StreamToStringRedirect outputStringRedirect = null;
    private ExecUtil.StreamToWriterRedirect outputWriterRedirect = null;

    /**
     * Execute a process. Redirect asynchronously stdout to a string and stderr
     * to nowhere. Use only for small outputs, otherwise use the execute()
     * variant with Writer.
     *
     * @param aCommand command to be executed
     * @param params parameters of the command
     * @return string buffer with captured stdout
     */
    public synchronized String execute(final String aCommand, final String... params) throws IOException, InterruptedException {
        String output = "";

        // build command array
        String[] arrayCommand = new String[params.length + 1];
        arrayCommand[0] = aCommand;

        StringBuilder arrayCommandToLog = new StringBuilder();
        arrayCommandToLog.append(aCommand).append(" ");

        for (int i = 1; i < arrayCommand.length; i++) {
            arrayCommand[i] = params[i - 1];
            arrayCommandToLog.append(arrayCommand[i]).append(" ");
        }

        final Runtime rt = Runtime.getRuntime();
        logger.log(Level.INFO, "Executing " + arrayCommandToLog.toString());

        proc = rt.exec(arrayCommand);

        //stderr redirect
        errorStringRedirect = new ExecUtil.StreamToStringRedirect(proc.getErrorStream(), "ERROR");
        //stdout redirect
        outputStringRedirect = new ExecUtil.StreamToStringRedirect(proc.getInputStream(), "OUTPUT");

        //start redurectors
        errorStringRedirect.start();
        outputStringRedirect.start();

        //wait for process to complete and capture error core
        final int exitVal = proc.waitFor();
        logger.log(Level.INFO, aCommand + " exit value: " + exitVal);

        errorStringRedirect.stopRun();
        errorStringRedirect = null;

        outputStringRedirect.stopRun();
        output = outputStringRedirect.getOutput();

        outputStringRedirect = null;

        //gc process with its streams
        //proc = null;

        return output;
    }

    /**
     * Execute a process. Redirect asynchronously stdout to a passed in writer
     * and stderr to nowhere.
     *
     * @param stdoutWriter file writer to write stdout to
     * @param aCommand command to be executed
     * @param params parameters of the command
     * @return string buffer with captured stdout
     */
    public synchronized void execute(final Writer stdoutWriter, final String aCommand, final String... params) throws IOException, InterruptedException {

        // build command array
        String[] arrayCommand = new String[params.length + 1];
        arrayCommand[0] = aCommand;

        StringBuilder arrayCommandToLog = new StringBuilder();
        arrayCommandToLog.append(aCommand).append(" ");

        for (int i = 1; i < arrayCommand.length; i++) {
            arrayCommand[i] = params[i - 1];
            arrayCommandToLog.append(arrayCommand[i]).append(" ");
        }

        final Runtime rt = Runtime.getRuntime();
        logger.log(Level.INFO, "Executing " + arrayCommandToLog.toString());

        proc = rt.exec(arrayCommand);

        //stderr redirect
        errorStringRedirect = new ExecUtil.StreamToStringRedirect(proc.getErrorStream(), "ERROR");
        //stdout redirect
        outputWriterRedirect = new ExecUtil.StreamToWriterRedirect(proc.getInputStream(), stdoutWriter);

        //start redurectors
        errorStringRedirect.start();
        outputWriterRedirect.start();

        //wait for process to complete and capture error core
        final int exitVal = proc.waitFor();
        logger.log(Level.INFO, aCommand + " exit value: " + exitVal);

        //gc process with its streams
        //proc = null;
    }

    /**
     * Interrupt the running process and stop its stream redirect threads
     */
    public synchronized void stop() {
        logger.log(Level.INFO, "Stopping Execution of: " + command);

        if (errorStringRedirect != null) {
            errorStringRedirect.stopRun();
{'fraction_non_alphanumeric': 0.09040894920227398, 'fraction_numerical': 0.0029341646799926646, 'mean_word_length': 2.893966440556944, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2270867', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2673, 'n_words': 926}