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

Anthony Vanelverdinghe duke at openjdk.org
Wed Mar 13 17:37:16 UTC 2024


On Tue, 12 Mar 2024 23:00:45 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: Revise handling of leading periods

Thanks for the responses/updates.

> > Why can't `Path::getExtension` be platform or provider dependent as well and specify something as follows?
> 
> Does this imply not having an extension if the file is hidden, or only ignoring a leading period (`charAt(0) == '.'`) if on UNIX?

Only ignoring a leading period (`charAt(0) == '.'`) if on UNIX.

The specification would be something like:

> The exact definition of file extension is platform or provider dependent. On UNIX for example a file is considered to be hidden if its name begins with a period character ('.') and this interpretation takes precedence over its interpretation as a file extension separator.

And I'd add an `@implSpec`/`@implNote` as well:

> Providers must/should ignore a leading period character as a file extension separator if and only if they consider a file with a leading period character to be hidden.

On another note, please have `withExtension(null)` throw a NullPointerException. `withExtension(ext)`, where ext can be `null`, should instead be written as `withExtension(Optional.ofNullable(ext).orElse(""))` to make it obvious that the treatment of `null` is intentional and not a bug.

Similarly, I believe it's worth considering to have `withExtension` throw an IllegalArgumentException if the `extension` parameter starts with a period character: this is almost certainly unintentional (e.g. due to passing on an extension as-is from an API that includes the period character) and a bug.

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

PR Comment: https://git.openjdk.org/jdk/pull/16226#issuecomment-1995113255


More information about the nio-dev mailing list