in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 103 104 105 106 107 108 < 109 > 110 111 112 113 114 115 .. 166 >> Next

The developer feverishly checks to determine if the slashes must go the other way, if the colon after the drive letter must come out, if a pipe character needs to substitute for the colon—anything to make the exception shown in Listing 34.1 go away.
Figure 34.1 JEditorPane loading normally.
The Mysterious File Protocol 299
5r? THst Web Browser ÍÏÎ
fie;/D:ienurferWet)Srte cliewir»g_on_niy_harid_i-5 jjre<ty_c«<ni -

Error retrieving specified URL è

Figure 34.2 Loading with file protocol misapplied.
01 D
02 at j ava:571)
03 at j ava:540)
04 at j ava:449)
05 at j<init>(Socket.j ava:100)
06 at ava:50)
07 at ava:38)
08 at 67)
09 at<init>(
20 at [...]
Listing 34.1 UnknownHostException
300 Item 34
The developer starts thinking: "UnknownHostException! That cannot be right. Shouldn't it be a MalformedURLException?" Desperate for a workaround, other measures are thrown around, "What if I test for the 'file://' and then strip the rest out and load it as a local file? That won't work . . . now I need to find another method to set the page, because setPage deals with URLs."
The developer takes another look at the URL class and sees this gem:
Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine. More information on the types of URLs and their formats can
The developer quickly browses over to confirm his understanding format of the file protocol, and this is what he finds:
File URLs
Suppose there is a document called "foobar.txt"; it sits on an anonymous ftp "" in directory "/pub/files". The URL for this file is then: file:// The toplevel directory of this FTP server is simply: file://
The "pub" directory of this FTP server is then: file:// That's all there is to it.2
"That's all there is to it!" If that isn't adding insult to injury! The information posted reminds the developer of two things: his understanding of the file protocol is correct and he is still at square one.
Desperate to make some sort of progress, the developer starts considering other options and inspecting the File class. In the File class there is a method called toURL(), which claims to convert the path and filename into a URL specifying the file protocol. The developer decides to add a JFileChooser into the mix and use the resulting selected file to create the URL:
try {
} catch (MalformedURLException malE) { malE.printStackTrace(); }
Figure 34.3 shows how that looks in the application.
After the developer gets this code up and running, the JFileChooser is now handling the URL specification for the application. This approach turns out to be a successful one, as shown in Figure 34.4.
be found at of the proper
server called
The Mysterious File Protocol 301
Figure 34.3 The JFileChooser strategy in action.
Figure 34.4 Properly loaded local file.
302 Item 35
Wanting to determine why the representation in the JTextField has only one slash and whether that would work if entered by hand, the developer performs some tests. He determines that it works without the slash at all and up to seven slashes— then realizes that the only number of slashes that doesn't work is the only number that should: two.
Why does this problem occur? Well, reviewing RFC2396 (URLs) more closely, we note a distinction is made between local and remote files. Absolute paths are annotated with single slashes, followed by the appropriate path segments, whereas remote paths are indicated by the double slashes. This is a common misunderstanding, driven out of the assumption that the file protocol specification refers only to the local filesystem. In fact, looking more closely at the previous example, we see the URL is actually pointing at a remote file server, an FTP server, and hence the two slashes. Note, however, that this subtle distinction is not made to the reader, nor is it prominent in the RFC.
Now that it has been established that the URL was wrong all along, why didn't a MalformedURLException get thrown when the developer was trying to create this URL? After all, this is an incorrect URL. Instead, unexpectedly an UnknownHostEx-ception is thrown. To understand this, the developer must consider the challenges of implementing such functionality. First, it is important to recognize that an Unknown-HostException is the result of being unable to resolve the host part of the URL to an IP address. As such, variability must be built in to allow for both hosts that do not exist and hosts that are simply unavailable. After all, how can it be determined that "vol1" is incorrectly referring to a local volume, as opposed to a file server on the network known as "vol1"? This requires the inconvenient problem of receiving an Unknown-HostException rather than a MalformedURLException.
Previous << 1 .. 103 104 105 106 107 108 < 109 > 110 111 112 113 114 115 .. 166 >> Next