Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4328499

URL.toString() leaves out slash between host and file

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P2 P2
    • 6-pool
    • 1.2.2
    • core-libs
    • generic
    • generic



      Name: skT45625 Date: 04/06/2000


      java version "1.2.2-RC2"
      Classic VM (build 1.2.2-RC2-K, green threads, javacomp)

      This bug is related to previous bug report #4269444

      Test code:
      ------------------------
      import java.net.*;

      class Test {
          public static final void main(String[] args)
      throws MalformedURLException
          {
      URL url = new URL("file", "localhost", "foobar");
      System.out.println(url);
          }
      }
      ------------------------
      Actual Output:
      file://localhostfoobar

      Expected Output:
      file://localhost/foobar

      Details:
      The toString() and toExternalForm() methods on java.net.URL and
      java.net.URLStreamHandler leave out the slash following the hostname in the
      string form. All are based on this implementation in URLStreamHandler.java:
      -----------------------------
          /**
           * Converts a <code>URL</code> of a specific protocol to a
           * <code>String</code>.
           *
           * @param u the URL.
           * @return a string representation of the <code>URL</code> argument.
           */
          protected String toExternalForm(URL u) {
      String result = u.getProtocol() + ":";
      if ((u.getHost() != null) && (u.getHost().length() > 0)) {
      result = result + "//" + u.getHost();
      if (u.getPort() != -1) {
      result += ":" + u.getPort();
      }
      }
      result += u.getFile();
      if (u.getRef() != null) {
      result += "#" + u.getRef();
      }
      return result;
          }
      ----------------------------
      It seems that this line:
      result += u.getFile();

      should be changed to include the slash:
      result += "/" + u.getFile();

      HOWEVER, a previous bug (#4269444) mentioned that the slash should be left out
      when there's no file in the URL. So the correct code should test this case:

      if ((u.getFile() != null) && (u.getFile() != ""))
        result += "/" + u.getFile();

      [I'm not sure that the null case is necessary]
      [Note the conditional changed from the original report]
      (Review ID: 103388)
      ======================================================================

            michaelm Michael McMahon
            skondamasunw Suresh Kondamareddy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: