import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 
 * @author Russell Glasser
 * 
 */
public class Utils {

	/**
	 * Load a web page and retrieve the content
	 * 
	 * @param urlt
	 *            Text of the URL to be retrieved
	 * @return html page content
	 */
	public static synchronized String getWebPage(String urlt) {
		String result = "";
		try {
			long t1 = System.currentTimeMillis();
			URL url = new URL(urlt);
			URLConnection conn = url.openConnection();
			DataInputStream input = new DataInputStream(conn.getInputStream());
			BufferedReader buf = new BufferedReader(
					new InputStreamReader(input));
			String inputLine;
			while ((inputLine = buf.readLine()) != null) {
				result += inputLine + "\n";
			}
			input.close();
			long t2 = System.currentTimeMillis();
			float elapsed = ((float) (t2 - t1) / 1000.f);
			result = url + " retrieved in " + elapsed + " seconds.\n\n"
					+ result;
		} catch (MalformedURLException e) {
			result = "Bad URL";
		} catch (IOException e) {
			result = "Failed to read data";
		}
		return result;
	}

}

