| 1 7 8 package java.net; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.util.Hashtable ; 14 import java.util.StringTokenizer ; 15 import sun.security.util.SecurityConstants; 16 17 116 public final class URL implements java.io.Serializable { 117 118 static final long serialVersionUID = -7627629688361524110L; 119 120 132 private static final String protocolPathProp = "java.protocol.handler.pkgs"; 133 134 138 private String protocol; 139 140 144 private String host; 145 146 150 private int port = -1; 151 152 157 private String file; 158 159 162 private transient String query; 163 164 168 private String authority; 169 170 173 private transient String path; 174 175 178 private transient String userInfo; 179 180 184 private String ref; 185 186 190 transient InetAddress hostAddress; 191 192 195 transient URLStreamHandler handler; 196 197 200 private int hashCode = -1; 201 202 280 public URL(String protocol, String host, int port, String file) 281 throws MalformedURLException  282 { 283 this(protocol, host, port, file, null); 284 } 285 286 304 public URL(String protocol, String host, String file) 305 throws MalformedURLException { 306 this(protocol, host, -1, file); 307 } 308 309 348 public URL(String protocol, String host, int port, String file, 349 URLStreamHandler handler) throws MalformedURLException { 350 if (handler != null) { 351 SecurityManager sm = System.getSecurityManager(); 352 if (sm != null) { 353 checkSpecifyHandler(sm); 355 } 356 } 357 358 protocol = protocol.toLowerCase(); 359 this.protocol = protocol; 360 if (host != null) { 361 362 366 if (host != null && host.indexOf(':') >= 0 367 && !host.startsWith("[")) { 368 host = "["+host+"]"; 369 } 370 this.host = host; 371 372 if (port < -1) { 373 throw new MalformedURLException ("Invalid port number :" + 374 port); 375 } 376 this.port = port; 377 authority = (port == -1) ? host : host + ":" + port; 378 } 379 380 Parts parts = new Parts(file); 381 path = parts.getPath(); 382 query = parts.getQuery(); 383 384 if (query != null) { 385 this.file = path + "?" + query; 386 } else { 387 this.file = path; 388 } 389 ref = parts.getRef(); 390 391 if (handler == null && 394 (handler = getURLStreamHandler(protocol)) == null) { 395 throw new MalformedURLException ("unknown protocol: " + protocol); 396 } 397 this.handler = handler; 398 } 399 400 412 public URL(String spec) throws MalformedURLException { 413 this(null, spec); 414 } 415 416 463 public URL(URL context, String spec) throws MalformedURLException { 464 this(context, spec, null); 465 } 466 467 487 public URL(URL context, String spec, URLStreamHandler handler) 488 throws MalformedURLException  489 { 490 String original = spec; 491 int i, limit, c; 492 int start = 0; 493 String newProtocol = null; 494 boolean aRef=false; 495 boolean isRelative = false; 496 497 if (handler != null) { 499 SecurityManager sm = System.getSecurityManager(); 500 if (sm != null) { 501 checkSpecifyHandler(sm); 502 } 503 } 504 505 try { 506 limit = spec.length(); 507 while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) { 508 limit--; } 510 while ((start < limit) && (spec.charAt(start) <= ' ')) { 511 start++; } 513 514 if (spec.regionMatches(true, start, "url:", 0, 4)) { 515 start += 4; 516 } 517 if (start < spec.length() && spec.charAt(start) == '#') { 518 522 aRef=true; 523 } 524 for (i = start ; !aRef && (i < limit) && 525 ((c = spec.charAt(i)) != '/') ; i++) { 526 if (c == ':') { 527 528 String s = spec.substring(start, i).toLowerCase(); 529 if (isValidProtocol(s)) { 530 newProtocol = s; 531 start = i + 1; 532 } 533 break; 534 } 535 } 536 537 protocol = newProtocol; 539 if ((context != null) && ((newProtocol == null) || 540 newProtocol.equalsIgnoreCase(context.protocol))) { 541 if (handler == null) { 544 handler = context.handler; 545 } 546 547 if (context.path != null && context.path.startsWith("/")) 552 newProtocol = null; 553 554 if (newProtocol == null) { 555 protocol = context.protocol; 556 authority = context.authority; 557 userInfo = context.userInfo; 558 host = context.host; 559 port = context.port; 560 file = context.file; 561 path = context.path; 562 isRelative = true; 563 } 564 } 565 566 if (protocol == null) { 567 throw new MalformedURLException ("no protocol: "+original); 568 } 569 570 if (handler == null && 573 (handler = getURLStreamHandler(protocol)) == null) { 574 throw new MalformedURLException ("unknown protocol: "+protocol); 575 } 576 577 this.handler = handler; 578 579 i = spec.indexOf('#', start); 580 if (i >= 0) { 581 ref = spec.substring(i + 1, limit); 582 limit = i; 583 } 584 585 589 if (isRelative && start == limit) { 590 query = context.query; 591 if (ref == null) { 592 ref = context.ref; 593 } 594 } 595 596 handler.parseURL(this, spec, start, limit); 597 598 } catch(MalformedURLException e) { 599 throw e; 600 } catch(Exception e) { 601 throw new MalformedURLException (e.getMessage()); 602 } 603 } 604 605 608 private boolean isValidProtocol(String protocol) { 609 int len = protocol.length(); 610 if (len < 1) 611 return false; 612 char c = protocol.charAt(0); 613 if (!Character.isLetter(c)) 614 return false; 615 for (int i = 1; i < len; i++) { 616 c = protocol.charAt(i); 617 if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' && 618 c != '-') { 619 return false; 620 } 621 } 622 return true; 623 } 624 625 628 private void checkSpecifyHandler(SecurityManager sm) { 629 sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION); 630 } 631 632 643 protected void set(String protocol, String host, 644 int port, String file, String ref) { 645 synchronized (this) { 646 this.protocol = protocol; 647 this.host = host; 648 authority = port == -1 ? host : host + ":" + port; 649 this.port = port; 650 this.file = file; 651 this.ref = ref; 652 654 hashCode = -1; 655 hostAddress = null; 656 int q = file.lastIndexOf('?'); 657 if (q != -1) { 658 query = file.substring(q+1); 659 path = file.substring(0, q); 660 } else 661 path = file; 662 } 663 } 664 665 680 protected void set(String protocol, String host, int port, 681 String authority, String userInfo, String path, 682 String query, String ref) { 683 synchronized (this) { 684 this.protocol = protocol; 685 this.host = host; 686 this.port = port; 687 this.file = query == null ? path : path + "?" + query; 688 this.userInfo = userInfo; 689 this.path = path; 690 this.ref = ref; 691 693 hashCode = -1; 694 hostAddress = null; 695 this.query = query; 696 this.authority = authority; 697 } 698 } 699 700 707 public String getQuery() { 708 return query; 709 } 710 711 718 public String getPath() { 719 return path; 720 } 721 722 728 public String getUserInfo() { 729 return userInfo; 730 } 731 732 738 public String getAuthority() { 739 return authority; 740 } 741 742 747 public int getPort() { 748 return port; 749 } 750 751 759 public int getDefaultPort() { 760 return handler.getDefaultPort(); 761 } 762 763 768 public String getProtocol() { 769 return protocol; 770 } 771 772 780 public String getHost() { 781 return host; 782 } 783 784 795 public String getFile() { 796 return file; 797 } 798 799 806 public String getRef() { 807 return ref; 808 } 809 810 835 public boolean equals(Object obj) { 836 if (!(obj instanceof URL )) 837 return false; 838 URL u2 = (URL )obj; 839 840 return handler.equals(this, u2); 841 } 842 843 851 public synchronized int hashCode() { 852 if (hashCode != -1) 853 return hashCode; 854 855 hashCode = handler.hashCode(this); 856 return hashCode; 857 } 858 859 870 public boolean sameFile(URL other) { 871 return handler.sameFile(this, other); 872 } 873 874 884 public String toString() { 885 return toExternalForm(); 886 } 887 888 898 public String toExternalForm() { 899 return handler.toExternalForm(this); 900 } 901 902 915 public URI toURI() throws URISyntaxException { 916 return new URI (toString()); 917 } 918 919 942 public URLConnection openConnection() throws java.io.IOException { 943 return handler.openConnection(this); 944 } 945 946 |