modules/media/src/main/java/com/sun/media/jfxmedia/locator/Locator.java

Print this page




 212         }
 213         scheme = scheme.toLowerCase();
 214 
 215         // Get the protocol.
 216         if (scheme.equals("jar")) {
 217             URI subURI = new URI(uriString.substring(4));
 218             protocol = subURI.getScheme();
 219             if (protocol == null) {
 220                 throw new IllegalArgumentException("uri.getScheme() == null! subURI == '" + subURI + "'");
 221             }
 222             protocol = protocol.toLowerCase();
 223         } else {
 224             protocol = scheme; // scheme is already lower case.
 225         }
 226 
 227         if (HostUtils.isIOS() && protocol.equals("ipod-library")) {
 228             isIpod = true;
 229         }
 230 
 231         // Verify the protocol is supported.
 232         if (!isIpod && !protocol.equals("file") && !protocol.equals("http")) {
 233             throw new UnsupportedOperationException("Unsupported protocol \"" + protocol + "\"");
 234         }
 235 
 236         // Check if we can block
 237         if (protocol.equals("http")) {
 238             canBlock = true;
 239         }
 240 
 241         // Set instance variable.
 242         this.uri = uri;
 243     }
 244 
 245     private InputStream getInputStream(URI uri)
 246             throws MalformedURLException, IOException {
 247         URL url = uri.toURL();
 248         URLConnection connection = url.openConnection();
 249 
 250         // Set request headers.
 251         synchronized (propertyLock) {
 252             if (connectionProperties != null) {
 253                 for (String key : connectionProperties.keySet()) {
 254                     Object value = connectionProperties.get(key);
 255                     if (value instanceof String) {
 256                         connection.setRequestProperty(key, (String) value);
 257                     }


 326 
 327     /*
 328      * Initialize locator. Use canBlock() to determine if init() can block.
 329      *
 330      * @throws URISyntaxException if the supplied URI requires some further
 331      * manipulation in order to be used and this procedure fails to produce a
 332      * usable URI. @throws IOExceptions if a stream cannot be opened over a
 333      * connection of the corresponding URL. @throws MediaException if the
 334      * content type of the media is not supported. @throws FileNotFoundException
 335      * if the media is not available.
 336      */
 337     public void init() throws URISyntaxException, IOException, FileNotFoundException {
 338         try {
 339             // Ensure the correct number of '/'s follows the ':'.
 340             int firstSlash = uriString.indexOf("/");
 341             if (firstSlash != -1 && uriString.charAt(firstSlash + 1) != '/') {
 342                 // Only one '/' after the ':'.
 343                 if (protocol.equals("file")) {
 344                     // Map file:/somepath to file:///somepath
 345                     uriString = uriString.replaceFirst("/", "///");
 346                 } else if (protocol.equals("http")) {
 347                     // Map http:/somepath to http://somepath
 348                     uriString = uriString.replaceFirst("/", "//");
 349                 }
 350             }
 351 
 352             // On non-Windows systems, replace "/~/" with home directory path + "/".
 353             if (System.getProperty("os.name").toLowerCase().indexOf("win") == -1
 354                     && protocol.equals("file")) {
 355                 int index = uriString.indexOf("/~/");
 356                 if (index != -1) {
 357                     uriString = uriString.substring(0, index)
 358                             + System.getProperty("user.home")
 359                             + uriString.substring(index + 2);
 360                 }
 361             }
 362 
 363             // Recreate the URI if needed
 364             uri = new URI(uriString);
 365 
 366             // First check if this URI is cached, if it is then we're done


 369                 // Cache hit! Grab contentType and contentLength and be done
 370                 contentType = cacheEntry.getMIMEType();
 371                 contentLength = cacheEntry.getBuffer().capacity();
 372                 if (Logger.canLog(Logger.DEBUG)) {
 373                     Logger.logMsg(Logger.DEBUG, "Locator init cache hit:"
 374                             + "\n    uri " + uri
 375                             + "\n    type " + contentType
 376                             + "\n    length " + contentLength);
 377                 }
 378                 return;
 379             }
 380 
 381             // Try to open a connection on the corresponding URL.
 382             boolean isConnected = false;
 383             boolean isMediaUnAvailable = false;
 384             boolean isMediaSupported = true;
 385             if (!isIpod) {
 386                 for (int numConnectionAttempts = 0; numConnectionAttempts < MAX_CONNECTION_ATTEMPTS; numConnectionAttempts++) {
 387                     try {
 388                         // Verify existence.
 389                         if (scheme.equals("http")) {
 390                             // Check ability to connect, trying HEAD before GET.
 391                             LocatorConnection locatorConnection = getConnection(uri, "HEAD");
 392                             if (locatorConnection == null || locatorConnection.connection == null) {
 393                                 locatorConnection = getConnection(uri, "GET");
 394                             }
 395 
 396                             if (locatorConnection != null && locatorConnection.connection != null) {
 397                                 isConnected = true;
 398 
 399                                 // Get content type.
 400                                 contentType = locatorConnection.connection.getContentType();
 401                                 contentLength = getContentLengthLong(locatorConnection.connection);
 402 
 403                                 // Disconnect.
 404                                 closeConnection(locatorConnection.connection);
 405                                 locatorConnection.connection = null;
 406                             } else if (locatorConnection != null) {
 407                                 if (locatorConnection.responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
 408                                     isMediaUnAvailable = true;
 409                                 }




 212         }
 213         scheme = scheme.toLowerCase();
 214 
 215         // Get the protocol.
 216         if (scheme.equals("jar")) {
 217             URI subURI = new URI(uriString.substring(4));
 218             protocol = subURI.getScheme();
 219             if (protocol == null) {
 220                 throw new IllegalArgumentException("uri.getScheme() == null! subURI == '" + subURI + "'");
 221             }
 222             protocol = protocol.toLowerCase();
 223         } else {
 224             protocol = scheme; // scheme is already lower case.
 225         }
 226 
 227         if (HostUtils.isIOS() && protocol.equals("ipod-library")) {
 228             isIpod = true;
 229         }
 230 
 231         // Verify the protocol is supported.
 232         if (!isIpod && !MediaManager.canPlayProtocol(protocol)) {
 233             throw new UnsupportedOperationException("Unsupported protocol \"" + protocol + "\"");
 234         }
 235 
 236         // Check if we can block
 237         if (protocol.equals("http") || protocol.equals("https")) {
 238             canBlock = true;
 239         }
 240 
 241         // Set instance variable.
 242         this.uri = uri;
 243     }
 244 
 245     private InputStream getInputStream(URI uri)
 246             throws MalformedURLException, IOException {
 247         URL url = uri.toURL();
 248         URLConnection connection = url.openConnection();
 249 
 250         // Set request headers.
 251         synchronized (propertyLock) {
 252             if (connectionProperties != null) {
 253                 for (String key : connectionProperties.keySet()) {
 254                     Object value = connectionProperties.get(key);
 255                     if (value instanceof String) {
 256                         connection.setRequestProperty(key, (String) value);
 257                     }


 326 
 327     /*
 328      * Initialize locator. Use canBlock() to determine if init() can block.
 329      *
 330      * @throws URISyntaxException if the supplied URI requires some further
 331      * manipulation in order to be used and this procedure fails to produce a
 332      * usable URI. @throws IOExceptions if a stream cannot be opened over a
 333      * connection of the corresponding URL. @throws MediaException if the
 334      * content type of the media is not supported. @throws FileNotFoundException
 335      * if the media is not available.
 336      */
 337     public void init() throws URISyntaxException, IOException, FileNotFoundException {
 338         try {
 339             // Ensure the correct number of '/'s follows the ':'.
 340             int firstSlash = uriString.indexOf("/");
 341             if (firstSlash != -1 && uriString.charAt(firstSlash + 1) != '/') {
 342                 // Only one '/' after the ':'.
 343                 if (protocol.equals("file")) {
 344                     // Map file:/somepath to file:///somepath
 345                     uriString = uriString.replaceFirst("/", "///");
 346                 } else if (protocol.equals("http") || protocol.equals("https")) {
 347                     // Map http:/somepath to http://somepath
 348                     uriString = uriString.replaceFirst("/", "//");
 349                 }
 350             }
 351 
 352             // On non-Windows systems, replace "/~/" with home directory path + "/".
 353             if (System.getProperty("os.name").toLowerCase().indexOf("win") == -1
 354                     && protocol.equals("file")) {
 355                 int index = uriString.indexOf("/~/");
 356                 if (index != -1) {
 357                     uriString = uriString.substring(0, index)
 358                             + System.getProperty("user.home")
 359                             + uriString.substring(index + 2);
 360                 }
 361             }
 362 
 363             // Recreate the URI if needed
 364             uri = new URI(uriString);
 365 
 366             // First check if this URI is cached, if it is then we're done


 369                 // Cache hit! Grab contentType and contentLength and be done
 370                 contentType = cacheEntry.getMIMEType();
 371                 contentLength = cacheEntry.getBuffer().capacity();
 372                 if (Logger.canLog(Logger.DEBUG)) {
 373                     Logger.logMsg(Logger.DEBUG, "Locator init cache hit:"
 374                             + "\n    uri " + uri
 375                             + "\n    type " + contentType
 376                             + "\n    length " + contentLength);
 377                 }
 378                 return;
 379             }
 380 
 381             // Try to open a connection on the corresponding URL.
 382             boolean isConnected = false;
 383             boolean isMediaUnAvailable = false;
 384             boolean isMediaSupported = true;
 385             if (!isIpod) {
 386                 for (int numConnectionAttempts = 0; numConnectionAttempts < MAX_CONNECTION_ATTEMPTS; numConnectionAttempts++) {
 387                     try {
 388                         // Verify existence.
 389                         if (scheme.equals("http") || scheme.equals("https")) {
 390                             // Check ability to connect, trying HEAD before GET.
 391                             LocatorConnection locatorConnection = getConnection(uri, "HEAD");
 392                             if (locatorConnection == null || locatorConnection.connection == null) {
 393                                 locatorConnection = getConnection(uri, "GET");
 394                             }
 395 
 396                             if (locatorConnection != null && locatorConnection.connection != null) {
 397                                 isConnected = true;
 398 
 399                                 // Get content type.
 400                                 contentType = locatorConnection.connection.getContentType();
 401                                 contentLength = getContentLengthLong(locatorConnection.connection);
 402 
 403                                 // Disconnect.
 404                                 closeConnection(locatorConnection.connection);
 405                                 locatorConnection.connection = null;
 406                             } else if (locatorConnection != null) {
 407                                 if (locatorConnection.responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
 408                                     isMediaUnAvailable = true;
 409                                 }