Przykład

public class ClassA {

public void makeStream (OutputStream oStream) {

try {
ZipOutputStream stream = new ZipOutputStream(oStream);
stream.write(7);
//inne operacje...

stream.close();

}
catch (IOException e) {
//przeciek...
}
}
}



Rozwiązanie
Aby uniknąć przecieków zasobów, upewnij się, że dla każdej klasy java.util.zip.ZipOutputStream jest wywoływana metoda close().
Uwaga: ta reguła ma zastosowanie do dowolnej postaci konstruktora ZipOutputStream.


public class ClassA {

public void makeStream (OutputStream oStream) {

ZipOutputStream stream = null;
try {
stream = new ZipOutputStream(oStream);
stream.write(7);
//inne operacje...

}
catch (IOException e) {
//inne operacje...

}
finally {
if (stream != null) {
try {
stream.close();

}
catch (IOException e) {
//inne operacje...

}
}
}
}
}