<commit_before>package net.thechunk.playpen.utils.process;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileProcessListener implements IProcessListener {
    private BufferedWriter writer = null;

    public FileProcessListener(File file) throws IOException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // quick, will probably change later
        Date date = new Date();

        writer = Files.newBufferedWriter(file.toPath(), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
        writer.write("-- FileProcessListener SESSION STARTED " + dateFormat.format(date) + "\r\n");
        writer.flush();
    }

    @Override
    public void onProcessAttach(XProcess proc) {
    }

    @Override
    public void onProcessDetach(XProcess proc) {
    }

    @Override
    public void onProcessOutput(XProcess proc, String out) {
        try {
            writer.write(out + "\n");
            writer.flush();
        }
        catch(IOException e) {}
    }

    @Override
    public void onProcessInput(XProcess proc, String in) {
        try {
            writer.write(in);
            writer.flush();
        }
        catch(IOException e) {}
    }

    @Override
    public void onProcessEnd(XProcess proc) {
        try {
            writer.write("-- FileProcessListener SESSION ENDED\r\n");
            writer.flush();
        }
        catch(IOException e) {}
        finally {
            try {
                writer.close();
            }
            catch(IOException e) {}
        }
    }
}<commit_msg>FileProcessListener will now asynchronously write output.<commit_after>package net.thechunk.playpen.utils.process;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileProcessListener implements IProcessListener {
    private Writer writer = null;
    private final ExecutorService service;

    public FileProcessListener(File file) throws IOException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // quick, will probably change later
        Date date = new Date();

        writer = new OutputStreamWriter(Files.newOutputStream(file.toPath(), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE));
        service = Executors.newSingleThreadExecutor();
        writer.write("-- FileProcessListener SESSION STARTED " + dateFormat.format(date) + "\r\n");
        writer.flush();
    }

    @Override
    public void onProcessAttach(XProcess proc) {
    }

    @Override
    public void onProcessDetach(XProcess proc) {
    }

    @Override
    public void onProcessOutput(XProcess proc, String out) {
        if (!service.isShutdown()) {
            service.execute(() -> {
                try {
                    writer.write(out);
                    writer.write(System.lineSeparator());
                } catch (IOException e) {
                }
            });
        }
    }

    @Override
    public void onProcessInput(XProcess proc, String in) {
        if (!service.isShutdown()) {
            service.execute(() -> {
                try {
                    writer.write(in);
                    writer.write(System.lineSeparator());
                } catch (IOException e) {
                }
            });
        }
    }

    @Override
    public void onProcessEnd(XProcess proc) {
        service.shutdownNow();
        try {
            writer.write("-- FileProcessListener SESSION ENDED\r\n");
            writer.flush();
        }
        catch(IOException e) {}
        finally {
            try {
                writer.close();
            }
            catch(IOException e) {}
        }
    }
}
{'fraction_non_alphanumeric': 0.10349926071956629, 'fraction_numerical': 0.0, 'mean_word_length': 2.168618266978923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4443548', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1064, 'n_words': 240}