rspec/rules/S4458/java/rule.adoc

52 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
In java 7 to 9, ``++FileInputStream++`` and ``++FileOutputStream++`` rely on finalization to perform final closes if the stream is not already closed. Whether or not the stream is already closed, the finalizer will be called, resulting in extra work for the garbage collector. This can easily be avoided using the ``++Files++`` API.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
try(FileInputStream fis = new FileInputStream(...)) { // Noncompliant
} finally {
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
2021-05-21 01:24:06 +00:00
try(InputStream is = Files.newInputStream(...)) {
2021-04-28 16:49:39 +02:00
} finally {
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use "Files.newXXXStream" here instead for better performance.
'''
== Comments And Links
(visible only on this page)
=== on 21 Feb 2019, 11:32:51 Tobias Gruetzmacher wrote:
Since I just debugged an issue with that class, I'd propose to add ``++java.util.zip.ZipFile++`` to the "dangerous" classes with finalizer... Using an ``++java.util.zip.ZipInputStream++`` avoids the finalizer.
=== on 21 Feb 2019, 11:56:44 Alexandre Gigleux wrote:
Hello [~tobix],
Can you open a thread on \https://community.sonarsource.com/c/suggestions/rules so we can get more details about the problem you faced? It's much more convenient than using JIRA Comments.
Thx
endif::env-github,rspecator-view[]