package djmania.file;

import java.io.FileInputStream;
import java.lang.System;

/**
 * (Helper) Assortment of helpful methods.
 * <p>
 * Assortment of helpful methods used by various classes, 
 * bundled here for convenience.
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Philip Peng
 * @version	1.0 27-Oct-07
 * 
 */

public final class FileTools {

	/**
	 * Creates a FileInputStream object from a String path.
	 * @param	file	String of file location.
	 * @return	FileInputStream of file.
	 */
	public static final FileInputStream convertStringToStream (String file)
	throws java.io.FileNotFoundException {
		FileInputStream stream = new FileInputStream(file);
		return stream;
	} // End convertToStream method
	
	/**
	 * Returns the file extension of a String path.
	 * @param	file	String of file location.
	 * @return	File extension or "UNKNOWN" if no file extension.
	 */
	public static final String getFileStringExtension (String file) {
		if (file.lastIndexOf('.')!=-1) {
			return file.substring(file.lastIndexOf('.')+1).toUpperCase();
		} else {
			return "UNKNOWN";
		}
	}
	
	/**
	 * Convenient shortcut for testing.
	 * @param	milliseconds	Time in milliseconds to sleep for.
	 */
	public static final void sleep(int milliseconds) {
		try {
			Thread.sleep(milliseconds);
		} catch (InterruptedException e) {
			System.err.println("Error: sleep thread interrupted.");
			System.err.println("Message: " + e.getMessage());
			System.err.println("Stack Trace: " + e.getStackTrace());
		}
	}
	
	/**
	 * Even more convenient shortcut for testing. 
	 * Sleeps for 3000 milliseconds (or 3 seconds).
	 */
	public static final void sleep() {
		sleep(3000);
	}
	
	/**
	 * Used by StepProcesser and subclasses.
	 * @param	buffer	Unprocessed buffer line.
	 * @return Trimmed buffer line.
	 */
	public static String bufferTrim(String buffer) {
		return buffer.substring(buffer.indexOf(':')+1);
	}
	
}
