Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.8.0] DM_DEFAULT_ENCODING for UTF-8 spec'd methods #2634

Closed
ben-manes opened this issue Oct 15, 2023 · 2 comments · Fixed by #2639
Closed

[4.8.0] DM_DEFAULT_ENCODING for UTF-8 spec'd methods #2634

ben-manes opened this issue Oct 15, 2023 · 2 comments · Fixed by #2639
Assignees

Comments

@ben-manes
Copy link

ben-manes commented Oct 15, 2023

The java.nio.file.Files methods readString(f) and writeString(f, str) are documented to use UTF-8 and do not rely on the default platform encoding. There is no need to specify the charset directly, unlike the legacy file i/o.

Code | Warning
-- | --
Dm | Found reliance on default encoding in com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat(): java.nio.file.Files.readString(Path)
  | Bug type DM_DEFAULT_ENCODING (click for details)In class com.github.benmanes.caffeine.cache.LocalCacheFactoryGeneratorIn method com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat()Called method java.nio.file.Files.readString(Path)At LocalCacheFactoryGenerator.java:[line 120]
Dm | Found reliance on default encoding in com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat(): java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])
  | Bug type DM_DEFAULT_ENCODING (click for details)In class com.github.benmanes.caffeine.cache.LocalCacheFactoryGeneratorIn method com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat()Called method java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])At LocalCacheFactoryGenerator.java:[line 122]
Dm | Found reliance on default encoding in com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat(): java.nio.file.Files.readString(Path)
  | Bug type DM_DEFAULT_ENCODING (click for details)In class com.github.benmanes.caffeine.cache.NodeFactoryGeneratorIn method com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat()Called method java.nio.file.Files.readString(Path)At NodeFactoryGenerator.java:[line 123]
Dm | Found reliance on default encoding in com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat(): java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])
  | Bug type DM_DEFAULT_ENCODING (click for details)In class com.github.benmanes.caffeine.cache.NodeFactoryGeneratorIn method com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat()Called method java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])At NodeFactoryGenerator.java:[line 125]

Code	Warning
Dm	Found reliance on default encoding in com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat(): java.nio.file.Files.readString(Path)
[Bug type DM_DEFAULT_ENCODING (click for details)](https://github.com/spotbugs/spotbugs/issues/new#DM_DEFAULT_ENCODING)
In class com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator
In method com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat()
Called method java.nio.file.Files.readString(Path)
At LocalCacheFactoryGenerator.java:[line 120]

Dm	Found reliance on default encoding in com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat(): java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])
[Bug type DM_DEFAULT_ENCODING (click for details)](https://github.com/spotbugs/spotbugs/issues/new#DM_DEFAULT_ENCODING)
In class com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator
In method com.github.benmanes.caffeine.cache.LocalCacheFactoryGenerator.reformat()
Called method java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])
At LocalCacheFactoryGenerator.java:[line 122]

Dm	Found reliance on default encoding in com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat(): java.nio.file.Files.readString(Path)
[Bug type DM_DEFAULT_ENCODING (click for details)](https://github.com/spotbugs/spotbugs/issues/new#DM_DEFAULT_ENCODING)
In class com.github.benmanes.caffeine.cache.NodeFactoryGenerator
In method com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat()
Called method java.nio.file.Files.readString(Path)
At NodeFactoryGenerator.java:[line 123]

Dm	Found reliance on default encoding in com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat(): java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])
[Bug type DM_DEFAULT_ENCODING (click for details)](https://github.com/spotbugs/spotbugs/issues/new#DM_DEFAULT_ENCODING)
In class com.github.benmanes.caffeine.cache.NodeFactoryGenerator
In method com.github.benmanes.caffeine.cache.NodeFactoryGenerator.reformat()
Called method java.nio.file.Files.writeString(Path, CharSequence, OpenOption[])
At NodeFactoryGenerator.java:[line 125]
@ben-manes
Copy link
Author

Please revert #2516. It was completely wrong in it’s premise.

gtoison added a commit to gtoison/spotbugs that referenced this issue Oct 16, 2023
Contrary to what was repored in spotbugs#2114 and as noted in spotbugs#2634, methods
added in JDK 11 are using UTF-8 (not the default charset)
@iloveeclipse
Copy link
Member

For the completeness, link to Java 11 API: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#readString(java.nio.file.Path)
which is (no surprise) still same in https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#readString(java.nio.file.Path).

Quote

public static String readString​(Path path) throws IOException
Reads all content from a file into a string, decoding from bytes to characters using the UTF-8 charset. The method ensures that the file is closed when all content have been read or an I/O error, or other runtime exception, is thrown.
This method is equivalent to: readString(path, StandardCharsets.UTF_8)

iloveeclipse pushed a commit that referenced this issue Oct 16, 2023
Contrary to what was repored in #2114 and as noted in #2634, methods
added in JDK 11 are using UTF-8 (not the default charset)
@iloveeclipse iloveeclipse added this to the SpotBugs 5.0.0 milestone Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants