Beginning Java 2- JDK 1.3 Version
Images and Animation
Identifying Sources on a Network
Data sources on a network are
identified by a Uniform Resource Locator,
or URL.
A source identified by a URL can be a file, or can be some other facility that
makes data available, such as a search engine for instance. A URL is a
relatively straightforward extension of the normal way of identifying a data
source file by the path. It just has some extra bits to identify the computer
that contains the source, and how you should communicate with the computer to
get at the file. A URL is made up of four pieces of information, some of which
may be omitted entirely or take default values:
| Protocol | Domain Name | Port Number | Path and File Name |
| http:// | www.wrox.com | :80 | /index.html |
| ftp:// | java.sun.com | :21 | |
The protocol identifies the particular communications method
that is to be used to get at the file. HTTP
is the HyperText Transfer Protocol
used on the World Wide Web, FTP
is the File Transfer Protocol, and
there are others (we'll see one in the next chapter).
The domain name identifies the data source uniquely. The
last part of the name identifies the kind of organization that owns the
computer. For instance, a domain name ending in .com is usually a
commercial organization in the USA such as Wrox Press or Sun, .co.uk
is a UK company, .edu
is an educational establishment in the USA, .ac.uk is an academic or
research organization in the UK and .gov is a government or
other public body. These are just a few examples. There are lots of others.
A port is just a number identifying a particular service on
a computer. A computer can have many ports – each providing a different
service. You don't usually need to specify the port number in a URL because a
standard port number, which is typically associated with a particular protocol,
will be assigned by default. The port number for the http protocol is 80 for
example, and the port number for ftp is 21.
Because the domain name is part of the reference to a data
source, every data source in a network has a unique URL. As long as you know
the URL for the source of the data you want on the Internet, and the machine
containing it is connected and active, you can go straight to it. Assuming, of
course, you have permission!
The URL Class
As you saw when we discussed ImageIcon
constructors, the URL
class encapsulates URLs. This class is defined in the java.net package, so you
will need an import
statement for the package or for the class when you refer to the URL
class in your code. A URL
object identifies a particular URL and provides you with the tools to access it
and read it, wherever it is on the network. You can create a URL
object from a string specifying the URL, for example:
URL sourceURL = new URL("http://www.wrox.com/");
This defines a URL object corresponding
to the home (or default) page on the Wrox web site.
Perhaps the most commonly used
URL
constructor accepts two arguments – a URL object identifying an
Internet source, plus a String
argument which is a specification for a source within the context of the URL
specified by the first argument. The popularity of this form is due to the
definition within the Applet
class of a method, getCodeBase(), which
returns the URL
for the source where the .class
file for the applet is stored. This enables you to create the URL
for a file, picture.gif
say, which is stored in the same directory as the applet code, wherever it is,
with a statement such as:
URL getThePicture = new URL(getCodeBase(), "picture.gif");
There is another method in the Applet class (and
therefore inherited in the JApplet
class) that provides a useful URL object. This is the getDocumentBase() method
that returns a URL
object corresponding to the URL for the document (the .html file in other
words) that contains the applets. This may well be different from the location
of the applet itself. You could call this method to obtain the URL like this:
URL doc = getDocumentBase(); // Get the document containing the applet
You can also define a URL object by supplying a
constructor with separate values for the protocol, the host name, the port
number and the source name. You specify the port number as type int,
and the other arguments as type String. The web site defined by the domain
name "www.ncsa.uiuc.edu"
contains a page that explains what a URL is. The file path and name for the
page is "/demoweb/url-primer.html".
You could define a URL
object for this page with the statement:
URL sourceURL = new URL("http", // Protocol
"www.ncsa.uiuc.edu", // Host name
"/demoweb/url-primer.html"); // File
This uses the default port number for the protocol. You just
need to supply the protocol, the domain name and the file path as String
objects. Note that URL paths are always specified using forward slashes as
separators, regardless of the type of server hosting the data source.
There is an alternative constructor defined in the URL
class:
URL sourceURL = new URL("http", // Protocol
quot;www.ncsa.uiuc.edu", // Host name
80, // Port number
"/demoweb/url-primer.html"); // File
The value of 80 for the port number is the standard port
number for http.
The value of -1 for the port name selects the default port number for the
protocol. If you specify a protocol that cannot be identified, a MalformedURLException
will be thrown.
Once you have a URL object, you can get
the components of the URL by calling the getProtocol(), getHost(),
getPort()
and getFile()
methods for the object. The getPort() method returns
the port number as type int,
and the other methods return objects of type String.
The URL
class also implements an equals() method that you
can use to compare two URL
objects. The expression URL1.equals(URL2)
will return true
if URL1
and URL2
specify the same file on the same host via the same port number and using the
same protocol.
Perhaps the most useful method in the URL
class is the openStream() method. This
returns an object of type InputStream,
which you can use to read the file represented by the URL. We can try
this out by reading the file referenced by the sourceURL object we
created in the code fragment above.
Try It Out – Reading a URL
This The following example assumes you have
a live connection to the Internet. So when the program closes, the connection
may still be open, in which case you will need to close it manually. Here's the
program to read a URL:
|
The program assumes that the directory JunkData that we used in
Chapter 8 is still on your C:
drive. If it isn't, you can add it, or better still, add the code to check that
the directory is there, and if it isn't, create it.
|
import java.net.*;
import java.io.*;
class ReadURL
{
public static void main(String[] args)
{
try
{
// Define a URL
URL sourceURL = new URL(
"http://www.ncsa.uiuc.edu/demoweb/url-primer.html");
// Get a character input stream for the URL
BufferedReader in = new BufferedReader(
new InputStreamReader(
sourceURL.openStream()));
// Create the stream for the output file
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(
new File("C:/JunkData/netdata.html"))));
System.out.println("Reading the file " + sourceURL.getFile() +
" on the host " + sourceURL.getHost() +
" using " + sourceURL.getProtocol());
// Read the URL and write it to a file
String buffer; // Buffer to store lines
while(!(null==(buffer=in.readLine())))
out.println(buffer);
in.close(); // Close the input stream
out.close(); // Close the output file
}
catch(MalformedURLException e)
{
System.out.println("Failed to create URL:\n" + e);
}
catch(IOException e)
{
System.out.println("File error:\n" + e);
}
}
}
While we have used a URL that references a source on the
Internet, you could also use a URL to specify a file on your local machine.
Executing this as written produces the output:
Reading the file /demoweb/url-primer.html on the host www.ncsa.uiuc.edu using http
It will also write the text contents of the URL to the file netdata.html.
Have a look at what you’ve captured. It’s a good read.
How It Works
The object sourceURL is created directly from the URL
string. Because the constructor can throw a MalformedURLException – if
you type the URL string incorrectly, for example – we have a catch
block for this exception following the try block. Note that this
exception is derived from IOException
so the catch
block for this must precede the catch block for IOException. Depending on whether your
dial-up-networking is working properly, another typical exception can be java.net.UnknownHostException.
By calling the openStream()
method for the URL
object, we obtain a stream that we can pass to the InputStreamReader
constructor. A character reader will automatically take care of converting
characters from the stream to Unicode. From the InputStreamReader we
create a BufferedReader
object so input from the stream will be buffered in memory.
The file is a sizable .html file, so rather
than writing it to the screen, we write it to a file in the JunkData
directory that we used in Chapter 8. You will then be able to view the file
using your web browser. Once we have the BufferedReader object for
the URL, the reading of the URL and writing the local file follows the sort of
process you have already seen in Chapter 8. We just use the readLine()
method to read a line at a time from the URL into the string buffer, and we use
the println()
method to transfer the buffer contents to the local file. You can now read more
about URLs by reading the file contents at your leisure. In the meantime, let's
get back to images.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.