RFR: 8298318: (fs) APIs for handling filename extensions [v4]

Louis Bergelson duke at openjdk.org
Thu Mar 7 20:35:54 UTC 2024


On Wed, 6 Mar 2024 23:41:05 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> Add to `java.nio.file.Path` a method `getExtension` to retrieve the `Path`'s extension, and companion methods `removeExtension` and `addExtension`.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8298318: Remove vestigial trim() call and commented out methods

src/java.base/share/classes/java/nio/file/Path.java line 324:

> 322:      * This method must satisfy the invariant:
> 323:      * {@snippet lang="java" :
> 324:      *     assert equals(Path.of(withoutExtension().toString()

I'm an author of an HTTP filesystem provider and would like to use these new methods since they address longstanding pain points working with Paths.  However, my usage wouldn't be able to satisfy this requirement. 

Here's an example of on our path strings:  `http://example.com/subfolder/file.txt?language=english`

In this case, the filename is `file.txt` and the query parameters (`?language=english`) are not considered part of the filename.
This invariant doesn't work for such a path.

withoutExtension() == "http://example.com/subfolder/file?language=english"
getExtension() == "txt"

So it would end up looking like:

Path.of("http://example.com/subfolder/file?language=english" + ".txt"). ==  "http://example.com/subfolder/file?language=englis.txt"


I think the invariant might be better written something like this:


path.equals(path.withExtension(path.getExtension())


As an aside, it's not generally true that `Path.of(path.toString()).equals(path)` so if you are going to require that you have to specify it only applies to the default FileSystemProvider.


To construct more general Path implementations you have to use the `Path.of(URI)` method instead of `Path.of(String)` so it would probably be better to refer to that.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/16226#discussion_r1516785398


More information about the nio-dev mailing list