/*
 * @(#)SystemUtilities.java    1.0  14 February 2001
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.util;

import java.awt.Toolkit;
import java.io.IOException;

/**
 * <P>Provides utility methods for gathering information
 * about the system and activating system components.</P>
 *
 * <P>This class contains an impure method,
 * by the 100% Pure Java standard.
 *
 * This impurity is acceptable, as it is covered by variances
 * 1, 5 as described in the <I>100% Pure Java Cookbook</I>.
 *
 * Please see the documentation for the
 * <CODE>{@link #execute(String) execute}</CODE> method
 * for more details.</P>
 *
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public class SystemUtilities {
    
    /**
     * Sounds the machine beep.
     */
    public static void beep() {
        Toolkit.getDefaultToolkit().beep();
    }

    /**
     * Spawns a new system process 
     * using the given command line.
     *
     * Please note that this process will not be
     * a lightweight Java thread, but rather
     * a full operating system process.
     *
     * This method is usually a violation of the
     * 100% Pure Java standard, but is protected
     * in the JPT by variances 1, 5, as this functionality
     * is included in the JPT as a convenience
     * to be used for building an operating system shell
     * or opening a browser window.
     *
     * @param command the full command line to execute
     * @exception IOException if there was an error
     *      spawning the process
     */
    public static void execute(String command) 
        throws IOException 
    {
        Runtime.getRuntime().exec(command);
    }
    
    /**
     * Returns the number of free bytes 
     * of system memory.
     *
     * @see #getTotalMemory()
     */
    public static long getFreeMemory() {
        return Runtime.getRuntime().freeMemory();
    }
    
    /**
     * Returns the platform specific line separator.
     *
     * If the line separator sequence for this system
     * is unavailable due to security restrictions,
     * the newline character, <CODE>'\n'</CODE>
     * is returned by default.
     */
    public static String getLineSeparator() {
        try {
            return System.getProperty("line.separator");
        }
        catch (Exception ex) {
            return "\n";
        }
    }
    
    /**
     * Returns the total number of bytes 
     * of system memory.
     *
     * @see #getFreeMemory()
     */
    public static long getTotalMemory() {
        return Runtime.getRuntime().totalMemory();
    }
}
