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

Support autoregistering of a Path underlying FileSystems

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE REQUEST :
      Currently if you define a project's resource like
      Path resource = Paths.get(getClass().getClassLoader().getResource("foo/resource").toUri())

      you face a problem when using Files utility class form within the jar.
      The Filesystem provider (in this case ZipFileSystem) is not registered by default, which means the whole path interface is useless and not useful at all, since Files can only be used on certain kinds of Path, but it only works by default with the File implementation of Path.


      JUSTIFICATION :
      Java world would benefit if we could use Path as the new abstraction for pointers to resources, not only File.

      One of the things that would get rid of the need for ClassPathResource, ResourceUtils and such, provided by 3rd party fframeworks, would be allowing temporary autoregister of FileSystems.

      For this reason alone, we need to keep File as the type for all our internal method signatures, which means we need to copy other kinds of Paths to a temporary ile before.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I dream of a world where defining a main/src/resource is as easy as

      Path projectResource =
      Paths.get(getClass().getClassLoader().getResource("foo/resource").toUri())

      public void methodWithBusinessLogic(Path abstractedResource){
         Files.readLines(projectResosurce).forEach(...)
      }

      The major problem I see currently is supporting things like Files.inputStream, since we need to close the loaded FS after. Well theres always the chance to bind the close() with the one in inputstream.
      ACTUAL -
      It throws a

      Exception in thread "main" java.nio.file.FileSystemNotFoundException
      at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
      at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)

      ---------- BEGIN SOURCE ----------
      As it only fails from within a jar, just package a jar with a resource in src/main/resources and then put that in a main

      Path resource = Paths.get(getClass().getClassLoader().getResource("myFile").toUri())
      Files.readAllLines(resource),
      ---------- END SOURCE ----------

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

              Created:
              Updated: