Przykład

public class ClassA {

public void makeStream (OutputStream oStream) {

try {
BufferedOutputStream stream = new BufferedOutputStream(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.io.BufferedOutputStream jest wywoływana metoda close().
Uwaga: ta reguła ma zastosowanie do dowolnej postaci konstruktora BufferedOutputStream.


public class ClassA {

public void makeStream (OutputStream oStream) {

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

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

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

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

}
}
}
}
}