package internet;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * A class that retrieves text files over the Internet
 * using HTTP.
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class HTTPFileRetriever
{
    private List<String>    lines;

    /**
     * Explicit Value Constructor
     *
     * @param url  The URL of the file (including protocol)
     */
    public HTTPFileRetriever(String url)
    {
	BufferedReader  in;
	String          line;
	URL             u;

	try 
        {
	    u = new URL(url);

	    in = new BufferedReader(
		     new InputStreamReader(
			 u.openStream()));
	    
	    lines = new LinkedList<String>();
	    while ( (line=in.readLine()) != null) 
            {
		try 
                {
		    lines.add(URLDecoder.decode(line, "UTF-8"));
		}
                catch (Exception de) 
                {
		    // Ignore it
		}
	    }

	    in.close();
	}
        catch (MalformedURLException mue)
        {
	    // Do nothing
	    System.err.println(mue);
	}
        catch (IOException ioe) 
        {
	    // Do nothing
	    System.err.println(ioe);
	}
    }

    /**
     * Get the lines in the file.
     *
     * @return  The linesi in the file
     */
    public List<String> get()
    {
        return lines;
    }
}
