<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cyberborean Chronicles &#187; url</title>
	<atom:link href="http://blog.cyberborean.org/tag/url/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.cyberborean.org</link>
	<description>by Alex Alishevskikh</description>
	<lastBuildDate>Wed, 18 Jan 2012 07:52:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>URL&#8217;s in Java: Another pitfall</title>
		<link>http://blog.cyberborean.org/2007/08/22/urls-in-java-another-pitfall</link>
		<comments>http://blog.cyberborean.org/2007/08/22/urls-in-java-another-pitfall#comments</comments>
		<pubDate>Wed, 22 Aug 2007 12:58:52 +0000</pubDate>
		<dc:creator>Alex Alishevskikh</dc:creator>
				<category><![CDATA[Essays]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://cyberborean.wordpress.com/2007/08/22/urls-in-java-another-pitfall/</guid>
		<description><![CDATA[If ClassLoader Delegation, though esoteric and counterintuitive, is an example of some design, there are some things in Java that are confusing and just poorly designed at once. When testing my new Java application, I noticed strange freezing sometimes. It happened irregularly and the program did nothing serious at that time &#8211; no complex calculations, [...]]]></description>
			<content:encoded><![CDATA[<p><em>If <a href="http://cyberborean.wordpress.com/2007/07/04/custom-classloaders-the-black-art-of-java/">ClassLoader Delegation</a>, though esoteric and counterintuitive, is an example of some design, there are some things in Java that are  confusing and just poorly designed at once.</em></p>
<p><span id="more-174"></span></p>
<p>When testing my new Java application, I noticed strange freezing sometimes. It happened irregularly and the program did nothing serious at that time &#8211; no complex calculations, no heavy data transfers, etc. Nevertheless, I noticed that those freezings where related to my internet connection glitches &#8211; unfortunately, it occasionally happens here. But my program didn&#8217;t do anything with internet at those moments!</p>
<p>I did some profiling and found the bottleneck &#8211; to my surprise, it was <code>get()</code> method of a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashMap.html"><code>HashMap</code></a> instance. Sometimes it took about 20 seconds only for just checking if an object exists in the map. I was shocked. HashMaps! I used it for ages and was sure they are reasonably fast kind of collections.</p>
<p>One way or another, I found the problem. The point was that I used <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html"><code>java.net.URL</code></a> instances as the HashMap keys. <code>URL</code> objects in Java, in comparison with simple strings, are convenient as they store URLs as the sets of separate fields (protocol, host, path etc), guarantee URL well-formedness and enable to create a connection with a single <code>connect()</code> method.</p>
<p>But its implementation is horrible. As it <a href="http://kickjava.com/src/java/net/URL.java.htm">turned out</a>, two generic object methods &#8211; <code>equals()</code> and <code>hashCode()</code> needed for internet connection and domain name resolving just to check equality of two URLs or to compute object&#8217;s hash code.</p>
<blockquote><p>Two URL objects are equal if they have the same protocol, reference equivalent hosts, have the same port number on the host, and the same file and fragment of the file.</p>
<p>Two hosts are considered equivalent if both host names can be resolved into the same IP addresses; else if either host name can&#8217;t be resolved, the host names must be equal without regard to case; or both host names equal to null.</p>
<p>Since hosts comparison requires name resolution, this operation is a blocking operation.</p>
<p>(<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html"><code>java.net.URL</code> javadoc</a>)</p></blockquote>
<p>I have no idea what were the reasons to do that, but it is bad. Simply bad. Such common operations as equality test (and hash code computation) should rely on an object data only. Making it dependent on external unreliable resources is dangerous. Also, those operations must be fast as possible, because they are called frequently (often inside the loops). When the time of their execution is equal to the connection time-out, your program simply don&#8217;t work.</p>
<p>Even if there were some reasons for that, it works wrong.</p>
<blockquote><p>Note: The defined behavior for equals is known to be inconsistent with virtual hosting in HTTP.</p>
<p>(<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html">Ibid.</a>)</p></blockquote>
<p>Sure. For instance, URLs of all WordPress blogs are equal from point of view of <code>java.net.URL</code>, as long as all of them are resolved to the same IP address. And a whole lot of other web-sites, unless they are hosted on the dedicated servers.</p>
<p>URLs are just the text strings with known syntax. They should work offline, no matter they are &#8220;http:&#8221; or &#8220;file:&#8221;. They don&#8217;t need to be resolved to the IP addresses, unless a referenced resource is requested.</p>
<p>Do not use <code>java.net.URL</code>. Store URLs as simple String objects. If you need to keep URLs structured and well-formed, you can use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/URI.html"><code>java.net.URI</code></a> class instead, but the strings are faster anyway. You always can parse a string into <code>java.net.URI</code> object when it is needed. You can parse it into <code>java.net.URL</code> as well, but.. better don&#8217;t do it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyberborean.org/2007/08/22/urls-in-java-another-pitfall/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

