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

URI constructor violates guaranteed identity for URI with [brackets] in path

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • tbd
    • 7, 9
    • core-libs
    • x86
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.7.0_04"
      Java(TM) SE Runtime Environment (build 1.7.0_04-b22)
      Java HotSpot(TM) Client VM (build 23.0-b21, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      The 3-args java.net.URI constructor throws an URISyntaxException and thus violates an identity that is guaranteed in the Javadoc of the URI class:

      "For any URI u [..] the following identities also hold:
      new URI(u.getScheme(),
              u.getSchemeSpecificPart(),
              u.getFragment())
      .equals(u) "

      For
              URI u= new File("C:\\t\\abc[].zip").toURI();
      an exception is wrongly thrown.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      "true" should be printed.

      The schemeSpecificPart should be properly encoded and the URI "file:/C:/t/abc%5B%5D.zip" should be created.
      ACTUAL -
      Exception in thread "main" java.net.URISyntaxException: Illegal character in path at index 14: file:/C:/t/abc[].zip
      at java.net.URI$Parser.fail(URI.java:2829)
      at java.net.URI$Parser.checkChars(URI.java:3002)
      at java.net.URI$Parser.parseHierarchical(URI.java:3086)
      at java.net.URI$Parser.parse(URI.java:3034)
      at java.net.URI.<init>(URI.java:824)
      at bug.URIBug.main(URIBug.java:12)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.File;
      import java.net.URI;
      import java.net.URISyntaxException;

      public class URIBug {
          public static void main(String[] args) throws URISyntaxException {
              URI u= new File("C:\\t\\abc[].zip").toURI();
              /*
               * Throws a URISyntaxException and thus violates this identity
               * that is guaranteed in the Javadoc of class java.net.URI:
               */
              boolean test= new URI(u.getScheme(),
                      u.getSchemeSpecificPart(),
                      u.getFragment())
                      .equals(u);
              System.out.println(test);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use the 5- or 7-arg constructor of URI.

      BTW: The third of the given identities is wrong:
      "u.getUserInfo(), " must be removed here:

      new URI(u.getScheme(),
              u.getUserInfo(), u.getAuthority(),
              u.getPath(), u.getQuery(),
              u.getFragment())
      .equals(u)

            dfuchs Daniel Fuchs
            coffeys Sean Coffey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: