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

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

/** Read a remote file using the standard URL class
 *  instead of connecting explicitly to the HTTP server.
 */

public class UrlRetriever2 {
  public static void main(String[] args) {
    checkUsage(args);
    try {
      URL url = new URL(args[0]);
      BufferedInputStream buffer =
        new BufferedInputStream(url.openStream());
      DataInputStream in =
        new DataInputStream(buffer);
      String line;
      while ((line = in.readLine()) != null)
        System.out.println("> " + line);
      in.close();
    } catch(MalformedURLException mue) { // URL c'tor
      System.out.println(args[0] + "is an invalid URL: "
                         + mue);
    } catch(IOException ioe) { // Stream constructors
      System.out.println("IOException: " + ioe);
    }
  }

  private static void checkUsage(String[] args) {
    if (args.length != 1) {
      System.out.println("Usage: UrlRetriever2 <URL>");
      System.exit(-1);
    }
  }
}

