72 lines
2.0 KiB
Plaintext
72 lines
2.0 KiB
Plaintext
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
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* 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)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|