<commit_before>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME>
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.core.lang;

import jlibs.core.io.IOUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.StringTokenizer;

/**
 * @author <NAME>
 */
public class RuntimeUtil{
    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    public static String getPID() throws IOException{
        String pid = System.getProperty("pid"); //NOI18N
        if(pid==null){
            String cmd[];
            File tempFile = null;
            try{
                if(System.getProperty("os.name").toLowerCase().indexOf("windows")==-1)
                    cmd = new String[]{ "/bin/sh", "-c", "echo $$ $PPID" }; //NOI18N
                else{
                    // getpids.exe is taken from http://www.scheibli.com/projects/getpids/index.html (GPL)
                    tempFile = File.createTempFile("getpids", "exe"); //NOI18N

                    // extract the embedded getpids.exe file from the jar and save it to above file
                    IOUtil.pump(RuntimeUtil.class.getResourceAsStream("getpids.exe"), new FileOutputStream(tempFile), true, true); //NOI18N
                    cmd = new String[]{ tempFile.getAbsolutePath() };
                }
                if(cmd!=null){
                    Process p = Runtime.getRuntime().exec(cmd);
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    IOUtil.pump(p.getInputStream(), bout, false, true);

                    StringTokenizer stok = new StringTokenizer(bout.toString());
                    stok.nextToken(); // this is pid of the process we spanned
                    pid = stok.nextToken();
                    System.setProperty("pid", pid); //NOI18N
                }
            }finally{
                if(tempFile!=null)
                    tempFile.delete();
            }
        }
        return pid;
    }

    /**
     * This method guarantees that garbage collection is
     * done unlike <code>{@link System#gc()}</code>
     */
    @SuppressWarnings({"UnusedAssignment"})
    public static void gc(){
        Object obj = new Object();
        WeakReference ref = new WeakReference<Object>(obj);
        obj = null;
        while(ref.get()!=null)
            System.gc();
    }

    /**
     * calls <code>{@link #gc()}</code> <code>count</code> times
     */
    public static void gc(int count){
        for(;count!=0; count--)
            gc();
    }

    /**
     * This method guarantees that garbage colleciton is
     * done after JVM shutdown is initialized
     */
    public static void gcOnExit(){
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run(){
                gc();
            }
        });
    }
}
<commit_msg>runCommand(...) added<commit_after>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME>
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.core.lang;

import jlibs.core.io.IOPump;
import jlibs.core.io.IOUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.StringTokenizer;

/**
 * @author <NAME>
 */
public class RuntimeUtil{
    public static String runCommand(String command, String[] envp, File workingDir) throws IOException{
        String cmd[];
        if(OS.get().isUnix())
            cmd = new String[]{ "sh", "-c", command };
        else
            cmd = new String[]{ "cmd", "/C", command };

        Process p = Runtime.getRuntime().exec(cmd, envp, workingDir);
        new Thread(new IOPump(p.getErrorStream(), System.err, false, false).asRunnable()).start();
        ByteArrayOutputStream bout = IOUtil.pump(p.getInputStream(), false);
        try{
            p.waitFor();
        }catch(InterruptedException ex){
            throw new RuntimeException("interrupted", ex);
        }
        if(p.exitValue()!=0)
            throw new IOException("exitValue is "+p.exitValue());

        return bout.toString();
    }

    public static String runCommand(String command) throws IOException{
        return runCommand(command, null, null);
    }

    public static String getPID() throws IOException{
        String pid = System.getProperty("pid"); //NOI18N
        if(pid==null){
            String command;
            if(OS.get().isUnix())
                command = "echo $$ $PPID";
            else{
                // getpids.exe is taken from http://www.scheibli.com/projects/getpids/index.html (GPL)
                File tempFile = File.createTempFile("getpids", "exe"); //NOI18N

                // extract the embedded getpids.exe file from the jar and save it to above file
                IOUtil.pump(RuntimeUtil.class.getResourceAsStream("getpids.exe"), new FileOutputStream(tempFile), true, true); //NOI18N
                command = tempFile.getAbsolutePath();
                tempFile.deleteOnExit();
            }
            StringTokenizer stok = new StringTokenizer(runCommand(command));
            stok.nextToken(); // this is pid of the process we spanned
            pid = stok.nextToken();
            System.setProperty("pid", pid); //NOI18N
        }
        return pid;
    }

    /**
     * This method guarantees that garbage collection is
     * done unlike <code>{@link System#gc()}</code>
     */
    public static void gc(){
        Object obj = new Object();
        WeakReference ref = new WeakReference<Object>(obj);
        obj = null;
        while(ref.get()!=null)
            System.gc();
    }

    /**
     * calls <code>{@link #gc()}</code> <code>count</code> times
     */
    public static void gc(int count){
        for(;count!=0; count--)
            gc();
    }

    /**
     * This method guarantees that garbage colleciton is
     * done after JVM shutdown is initialized
     */
    public static void gcOnExit(){
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run(){
                gc();
            }
        });
    }
}

{'fraction_non_alphanumeric': 0.11663216011042098, 'fraction_numerical': 0.004692891649413389, 'mean_word_length': 2.171115973741794, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '3941461', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1939, 'n_words': 598}