/*
    This program opens a connection to the URL given as the first 
    argument, reads the text on the URL, and prints it to the standard 
    output. 

    Acknowledgement: This example is based on one in David Flanagan's 
        book _Java_in_a_Nutshell_  (O'Reilly & Associates, 1996).

    Written by:  H. Conrad Cunningham, 9 Sept 1996.
    Revised by:  H. Conrad Cunningham, 1 Oct 1996.
                 - inserted check for 1 command line argument
		 - changed variable "url" to "urlIn"

    Note:  18 January 1999.  This program should be modified to use the 
           Reader classes.
*/

import java.net.*;
import java.io.*;

public class GetURLText
{
    public static void printURL(URLConnection u) throws IOException 
    /*  Pre:   u is a valid connection to a URL.  The connected file
	           contains lines of text.
	Post:  The lines of text from u have been printed onto the standard
	           output stream.
	Note:  No exceptions are handled.
    */
    {   // Open data stream in on the URL connection u
        DataInputStream in = new DataInputStream(u.getInputStream());
 	// Copy lines of text from in to the standard output.
        String line;
        for(line = in.readLine(); line != null; line = in.readLine())
            System.out.println(line);

	// Close the input stream.
	in.close();
    }
    
    public static void main(String[] args) 
            throws MalformedURLException, IOException
    /*  Pre:   The command line argument denotes an accessible Universal
                   Resource Locator (URL) on the World-Wide Web containing 
	           lines of text.
        Post:  The lines of text from the URL have been printed to the 
                   standard output.
        Note:  No exceptions are handled.
    */
    {   if (args.length != 1)
            System.err.println("Usage: java GetURLText " + 
			       "<URL for html file>");
        else
        {   URL urlIn = new URL(args[0]);
            URLConnection connection = urlIn.openConnection();
	    printURL(connection);
        }
    }
}
