54 lines
1.6 KiB
Plaintext
54 lines
1.6 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
|
|
|
|
----
|
|
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
|
|
|
|
----
|
|
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
|
|
|
|
* Java Performance Tuning Guide - http://java-performance.info/java-io-bufferedinputstream-and-java-util-zip-gzipinputstream/[java.io.BufferedInputStream and java.util.zip.GZIPInputStream]
|
|
|