Another instance in which you might use streams is when you want to use networking in a Java program. In this context, networking means having your Java application or applet communicate to another system over a network, such as the Internet. Communication over the Internet is accomplished mostly through the class URL (Uniform Resource Locator), which is included in the java.net package.
The URL class has the following constructors:
URL(String). Tries to create a complete URL based on the string passed in.
URL(String, String, String). Attempts to create a URL for the specified protocol, host, and file name.
URL(String, String, Int, String). Attempts to create a URL for the specified protocol, host, port, and file name.
URL(URL, String). Attempts to create a URL for the specified base URL and a relative URL passed through as a string in the second parameter.
All of these constructors throw the exception MalformedURLException if an error occurs in building the URL.
The following methods are commonly used with URL objects:
GetContents(). Returns the contents of the URL
OpenConnection(). Attempts to open the connection to the URL
OpenStream(). Attempts to open an InputStream connection
Below is an example of a Java application that opens a URL connection to the specified site and then displays the HTML (Hypertext Markup Language) source listing from that site. This example uses two streams: one for the input from the keyboard (System.in), and the other for the communication with the URL.
/* Example of code to read in a URL's HTML and print it to the screen */ import java.net.*; import java.io.*; public class Jsap1004 { public static void main (String[] args) { // Declare some variables byte userInput[] = new byte[256]; URL ourUrl; String instr; URLConnection conn = null; Object urlContent; BufferedInputStream buf; int urlByte; String line; // Setup prompt for user input System.out.println("Enter URL to retrieve:"); // Get user input try { System.in.read(userInput); } catch (IOException e) { System.out.println("I/O Error:" + e.getMessage()); return; } instr = new String(userInput); // Write out the User Input and initialization message System.out.println("Validating URL..."); try { ourUrl = new URL(instr); } catch (MalformedURLException e) { System.out.println("Problem with entered URL"); System.out.println(e.getMessage()); return; } // Attempt to establish a connection to this URL try { ourUrl.openConnection(); } catch (IOException e) { System.out.println("Cannot establish connection to URL"); System.out.println(e.getMessage()); return; } // Read in the HTML and Print to the screen try { urlContent = ourUrl.getContent(); if ( !( urlContent instanceof BufferedInputStream )) { System.out.println("Unknown input from URL"); System.out.println(urlContent.getClass().getName()); return; } buf = (BufferedInputStream)urlContent; urlByte = buf.read(); while( urlByte != -1 ) { System.out.print((char)urlByte); urlByte = buf.read(); } } catch (IOException e) { System.out.println("Problems reading HTML"); System.out.println(e.getMessage()); return; } System.out.println("Program Finished"); } }
Figure 10.1 shows the program screen of the preceding example if you try to go to the URL www.primapub.com. (This URL actually redirects you to Prima's real home page, www.primapublishing.com.)
Figure 10.1: Output from the sample URL reading program
Team-Fly |