Why is creating a url (with the wrong url) time consuming in java?

EDIT: It turned out that this question was asked incorrectly. see my answer below . Please vote to close it.

For unit test, I need the new java.net.URL(String) constructor to MalformedURLException , but it takes a lot of time for what & mdash does; 2.5 seconds.

I understand that this is probably due to an attempt to create a protocol handler, so I tried to specify a protocol handler, for example:

 new URL(null); new URL("://"); new URL("/abc"); 

I briefly reviewed the implementation; it seems to be preparing a URL string to clear obvious errors. Why is it taking so long, even with zero?

+4
source share
5 answers

Could this be something else to delay? I can not reproduce this:

 public class UrlTest { static public void urltest(String s) { long start = System.currentTimeMillis(); try { new URL(s); System.out.println("successfully created URL after "+(System.currentTimeMillis()-start)+" milliseconds. URL: "+s); } catch(Exception e) { System.out.println(e.toString()+" after "+(System.currentTimeMillis()-start)+" milliseconds. URL: "+s); } } static public void main(String[] args) { System.out.println(System.getProperty("java.version")); urltest(null); urltest("://"); urltest("/abc"); } } 

Output:

 1.6.0_18-ea java.net.MalformedURLException after 0 milliseconds. URL: null java.net.MalformedURLException: no protocol: :// after 1 milliseconds. URL: :// java.net.MalformedURLException: no protocol: /abc after 0 milliseconds. URL: /abc 

Regarding equals() , this is from javadoc :

Two hosts are considered equivalent if both host names can be resolved to the same IP addresses; otherwise, if the host name cannot be resolved, the host names must be equal regardless of the case; or both host names are null.

Since name resolution is required to compare nodes, this operation is a lock.

Note. Certain behavior for equals, as you know, is incompatible with shared hosting in HTTP.

+1
source

Because the URL class has been going on for quite some time and is trying several different attempts to resolve this (illegal) Url. He even tries to download various system packages that are allowed using Urls content.

The exact procedure is outlined in the Javadoc URL constructor .

Since these actions involve accessing and loading various classes and files, first access to such illegal URLs can take quite a while.

+3
source

Maybe you can use a URI to improve performance?

Note. The URI class shields its component fields in certain circumstances. The recommended way to control the encoding and decoding of URLs is to use a URI and convert between the two classes using toURI () and URI.toURL ().

The URLEncoder and URLDecoder classes can also be used, but only for encoding an HTML form that does not match the encoding scheme defined in RFC2396.

From http://java.sun.com/j2se/1.5.0/docs/api/java/net/URL.html

"People are encouraged to use URIs for analyzing and comparing URIs, and leave the URL class to access the URI itself, get a handler for the protocol, interact with the protocol, etc. So, at this time, we don’t plan to change the behavior of URL.equals / hashCode, and we will leave the error opens before Tiger when we re-examine our options. "

From http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4434494 , 2001.

+1
source

Thank you for sharing the unit test results - you were right, the creation of a URL almost nothing happens. Comparison causes problems:

 Assert.assertThat(url, equalTo(new URL("http://aa"))); // about 2 seconds 

There seems to be a REALLY call to http://aa ! It seems that only with "localhost" is this fast:

 Assert.assertThat(url, equalTo(new URL("http://localhost"))); // a few ms 

The solution would be to compare the String representation:

 Assert.assertThat(url.toExternalForm(), equalTo(new URL("http://aa").toExternalForm())); 
+1
source

One step through him and see where he spends his time!

I expect it to try to search for DNS, and your DNS client or server is not configured correctly.

0
source

Source: https://habr.com/ru/post/1392048/


All Articles