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

ZipFileSystem.getPath("").getFileName() returns null instead of an empty string

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :


      A DESCRIPTION OF THE PROBLEM :
      When invoked on a ZipFileSystem, getPath("").getFileName() returns null instead of an empty string. The API specification for FileSystem::getPath states: "A Path representing an empty path is returned if first is the empty string and more does not contain any non-empty strings." Furthermore, the class documentation for Path states that "A Path is considered to be an empty path if it consists solely of one name element that is empty." In consequence, getPath("") should return a path with a single empty name (empty string). Lastly, the null result of getPath("").getFileName() is in contradiction to getPath("").getNameCount() (correctly) returning 1.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the provided Java program.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The expected output is:
      class jdk.nio.zipfs.ZipFileSystem
      class jdk.nio.zipfs.ZipPath
      1
      //

      ACTUAL -
      The actual output is:
      class jdk.nio.zipfs.ZipFileSystem
      class jdk.nio.zipfs.ZipPath
      1
      /null/


      ---------- BEGIN SOURCE ----------
      import java.io.IOException;
      import java.net.URI;
      import java.nio.file.FileSystem;
      import java.nio.file.FileSystems;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.util.Collections;
      import java.util.UUID;

      public final class TestCase
      {
          public static void main(String[] args) throws IOException
          {
              Path zipFile = Paths.get(
                  System.getProperty("java.io.tmpdir"),
                  UUID.randomUUID().toString() + ".zip");
              
              try (FileSystem zipFileSystem = FileSystems.newFileSystem(
                  URI.create("jar:" + zipFile.toUri()),
                  Collections.singletonMap("create", "true")))
              {
                  Path emptyPath = zipFileSystem.getPath("");
                  System.out.println(zipFileSystem.getClass()); // class jdk.nio.zipfs.ZipFileSystem
                  System.out.println(emptyPath.getClass()); // class jdk.nio.zipfs.ZipPath
                  System.out.println(emptyPath.getNameCount()); // 1
                  System.out.println("/" + emptyPath.getFileName() + "/"); // null <<<<< INCORRECT
              }
              finally
              {
                  Files.deleteIfExists(zipFile);
              }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Client code can check for getNameCount() == 1 in combination with java.util.Objects.toString(getFileName(), "").isEmpty() in order to identify an empty path.

      FREQUENCY : always


            lancea Lance Andersen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: