< prev index next >

src/java.base/share/classes/java/net/URL.java

Print this page

  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.net.spi.URLStreamHandlerProvider;
  32 import java.nio.file.Path;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import java.util.Hashtable;
  36 import java.io.InvalidObjectException;
  37 import java.io.ObjectStreamException;
  38 import java.io.ObjectStreamField;
  39 import java.io.ObjectInputStream.GetField;
  40 import java.util.Iterator;
  41 import java.util.Locale;
  42 import java.util.NoSuchElementException;
  43 import java.util.ServiceConfigurationError;
  44 import java.util.ServiceLoader;
  45 
  46 import jdk.internal.access.JavaNetURLAccess;
  47 import jdk.internal.access.SharedSecrets;

  48 import jdk.internal.misc.VM;
  49 import sun.net.util.IPAddressUtil;
  50 import sun.security.util.SecurityConstants;
  51 import sun.security.action.GetPropertyAction;
  52 
  53 /**
  54  * Class {@code URL} represents a Uniform Resource
  55  * Locator, a pointer to a "resource" on the World
  56  * Wide Web. A resource can be something as simple as a file or a
  57  * directory, or it can be a reference to a more complicated object,
  58  * such as a query to a database or to a search engine. More
  59  * information on the types of URLs and their formats can be found at:
  60  * <a href=
  61  * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
  62  * <i>Types of URL</i></a>
  63  * <p>
  64  * In general, a URL can be broken into several parts. Consider the
  65  * following example:
  66  * <blockquote><pre>
  67  *     http://www.example.com/docs/resource1.html

1325                         throw sce;
1326                     }
1327                 }
1328                 return true;
1329             }
1330 
1331             public boolean hasNext() {
1332                 return getNext();
1333             }
1334 
1335             public URLStreamHandlerProvider next() {
1336                 if (!getNext())
1337                     throw new NoSuchElementException();
1338                 URLStreamHandlerProvider n = next;
1339                 next = null;
1340                 return n;
1341             }
1342         };
1343     }
1344 
1345     // Thread-local gate to prevent recursive provider lookups
1346     private static ThreadLocal<Object> gate = new ThreadLocal<>();
1347 
1348     @SuppressWarnings("removal")
1349     private static URLStreamHandler lookupViaProviders(final String protocol) {
1350         if (gate.get() != null)
1351             throw new Error("Circular loading of URL stream handler providers detected");
1352 
1353         gate.set(gate);
1354         try {
1355             return AccessController.doPrivileged(
1356                 new PrivilegedAction<>() {
1357                     public URLStreamHandler run() {
1358                         Iterator<URLStreamHandlerProvider> itr = providers();
1359                         while (itr.hasNext()) {
1360                             URLStreamHandlerProvider f = itr.next();
1361                             URLStreamHandler h = f.createURLStreamHandler(protocol);
1362                             if (h != null)
1363                                 return h;
1364                         }
1365                         return null;
1366                     }
1367                 });
1368         } finally {
1369             gate.set(null);
1370         }
1371     }
1372 
1373     /**
1374      * Returns the protocol in lower case. Special cases known protocols
1375      * to avoid loading locale classes during startup.
1376      */
1377     static String toLowerCase(String protocol) {
1378         if (protocol.equals("jrt") || protocol.equals("file") || protocol.equals("jar")) {
1379             return protocol;
1380         } else {
1381             return protocol.toLowerCase(Locale.ROOT);
1382         }
1383     }
1384 
1385     /**
1386      * Non-overrideable protocols: "jrt" and "file"
1387      *
1388      * Character-based comparison for performance reasons; also ensures
1389      * case-insensitive comparison in a locale-independent fashion.

  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.InputStream;
  31 import java.net.spi.URLStreamHandlerProvider;
  32 import java.nio.file.Path;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import java.util.Hashtable;
  36 import java.io.InvalidObjectException;
  37 import java.io.ObjectStreamException;
  38 import java.io.ObjectStreamField;
  39 import java.io.ObjectInputStream.GetField;
  40 import java.util.Iterator;
  41 import java.util.Locale;
  42 import java.util.NoSuchElementException;
  43 import java.util.ServiceConfigurationError;
  44 import java.util.ServiceLoader;
  45 
  46 import jdk.internal.access.JavaNetURLAccess;
  47 import jdk.internal.access.SharedSecrets;
  48 import jdk.internal.misc.Gate;
  49 import jdk.internal.misc.VM;
  50 import sun.net.util.IPAddressUtil;
  51 import sun.security.util.SecurityConstants;
  52 import sun.security.action.GetPropertyAction;
  53 
  54 /**
  55  * Class {@code URL} represents a Uniform Resource
  56  * Locator, a pointer to a "resource" on the World
  57  * Wide Web. A resource can be something as simple as a file or a
  58  * directory, or it can be a reference to a more complicated object,
  59  * such as a query to a database or to a search engine. More
  60  * information on the types of URLs and their formats can be found at:
  61  * <a href=
  62  * "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html">
  63  * <i>Types of URL</i></a>
  64  * <p>
  65  * In general, a URL can be broken into several parts. Consider the
  66  * following example:
  67  * <blockquote><pre>
  68  *     http://www.example.com/docs/resource1.html

1326                         throw sce;
1327                     }
1328                 }
1329                 return true;
1330             }
1331 
1332             public boolean hasNext() {
1333                 return getNext();
1334             }
1335 
1336             public URLStreamHandlerProvider next() {
1337                 if (!getNext())
1338                     throw new NoSuchElementException();
1339                 URLStreamHandlerProvider n = next;
1340                 next = null;
1341                 return n;
1342             }
1343         };
1344     }
1345 
1346     // gate to prevent recursive provider lookups
1347     private static final Gate LOOKUP_GATE = Gate.create();
1348 
1349     @SuppressWarnings("removal")
1350     private static URLStreamHandler lookupViaProviders(final String protocol) {
1351         if (!LOOKUP_GATE.tryEnter())
1352             throw new Error("Circular loading of URL stream handler providers detected");


1353         try {
1354             return AccessController.doPrivileged(
1355                 new PrivilegedAction<>() {
1356                     public URLStreamHandler run() {
1357                         Iterator<URLStreamHandlerProvider> itr = providers();
1358                         while (itr.hasNext()) {
1359                             URLStreamHandlerProvider f = itr.next();
1360                             URLStreamHandler h = f.createURLStreamHandler(protocol);
1361                             if (h != null)
1362                                 return h;
1363                         }
1364                         return null;
1365                     }
1366                 });
1367         } finally {
1368             LOOKUP_GATE.exit();
1369         }
1370     }
1371 
1372     /**
1373      * Returns the protocol in lower case. Special cases known protocols
1374      * to avoid loading locale classes during startup.
1375      */
1376     static String toLowerCase(String protocol) {
1377         if (protocol.equals("jrt") || protocol.equals("file") || protocol.equals("jar")) {
1378             return protocol;
1379         } else {
1380             return protocol.toLowerCase(Locale.ROOT);
1381         }
1382     }
1383 
1384     /**
1385      * Non-overrideable protocols: "jrt" and "file"
1386      *
1387      * Character-based comparison for performance reasons; also ensures
1388      * case-insensitive comparison in a locale-independent fashion.
< prev index next >