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

java.io.File(URI) throws IllegalArgumentException when using UNC notation

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      The implementation in constructor of File(URI) throws an IllegalArgumentException("URI has an authority component") when using a pathname in UNC notation (e.g. "//nas/root"). The bug is, that the URI authority for schema "file:" is part of the path.
      Using File("//nas/root") works as expected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      File file = new File(new URL("//nas/root").toURI());


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      file is valid
      ACTUAL -
      IllegalArgumentException("URI has an authority component")

      ---------- BEGIN SOURCE ----------
      File file = new File(new URL("//nas/root").toURI());
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      in File(URI):
              // Check our many preconditions
              if (!uri.isAbsolute())
                  throw new IllegalArgumentException("URI is not absolute");
              if (uri.isOpaque())
                  throw new IllegalArgumentException("URI is not hierarchical");
              String scheme = uri.getScheme();
              if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
                  throw new IllegalArgumentException("URI scheme is not \"file\"");
              /* bug: omit this statement
              if (uri.getRawAuthority() != null)
                  throw new IllegalArgumentException("URI has an authority component");
               */
              if (uri.getRawFragment() != null)
                  throw new IllegalArgumentException("URI has a fragment component");
              if (uri.getRawQuery() != null)
                  throw new IllegalArgumentException("URI has a query component");
              
              // and here the correction
              String uncPath;
              if (uri.getRawAuthority() == null) {
               // the old behaviour
                  uncPath = uri.getPath();
                  if (uncPath.isEmpty())
                      throw new IllegalArgumentException("URI path component is empty");
              }else {
               // the corrected behaviour with an UNC path
               // remember: URI is a file:
               uncPath = "//" + uri.getRawAuthority() + uri.getPath();
              }
             // contiunue the code...

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: