rspec/rules/S5764/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

84 lines
2.4 KiB
Plaintext

== Why is this an issue?
The class ``++java.util.zip.GZIPInputStream++`` is already buffering its input while reading. Thus passing a ``++java.io.BufferedInputStream++`` to a ``++java.util.zip.GZIPInputStream++`` is redundant. It is more efficient to directly pass the original input stream to ``++java.util.zip.GZIPInputStream++``.
Note that the default buffer size of ``++GZIPInputStream++`` is not the same as the one in ``++BufferedInputStream++``. Configure it if need be.
This rule raises an issue when a ``++java.util.zip.GZIPInputStream++`` reads from a ``++java.io.BufferedInputStream++``.
=== Noncompliant code example
[source,java]
----
import java.io.*;
import java.util.zip.GZIPInputStream;
public class Noncompliant {
void deflateFile(final File file) throws IOException {
try (
FileInputStream fileStream = new FileInputStream(file);
BufferedInputStream bufferedStream = new BufferedInputStream(fileStream);
InputStream input = new GZIPInputStream(bufferedStream); // Noncompliant
) {
// process the input
}
}
}
----
=== Compliant solution
[source,java]
----
import java.io.*;
import java.util.zip.GZIPInputStream;
public class Compliant {
void deflateFile(final File file) throws IOException {
try (
FileInputStream fileStream = new FileInputStream(file);
InputStream input = new GZIPInputStream(fileStream);
) {
// process the input
}
}
}
----
== Resources
* Stackoverflow question about usage of GZIPInputStream and the BufferedInputStream - https://stackoverflow.com/questions/4438085/seeking-out-the-optimum-size-for-bufferedinputstream-in-java/4438217#4438217[GZIPInputStream and the BufferedInputStream]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove the intermediate BufferedInputStream
=== Highlighting
Primary location: the GZIPInputStream instantiation
Secondary location: the BufferedInputStream instantiation.
'''
== Comments And Links
(visible only on this page)
=== on 9 Jul 2020, 09:37:33 Nicolas Harraudeau wrote:
\[~pierre-loup.tristant] There are a few references in other articles but this one explains it the best so I would prefer to keep it if possible. Otherwise I'll replace it with https://stackoverflow.com/a/4438217[this stack overflow link]
endif::env-github,rspecator-view[]