Przykład

public class ClassA {

public void makeStream (InputStream iStream) {

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


public class ClassA {

public void makeStream (InputStream iStream) {

BufferedInputStream stream = new BufferedInputStream(iStream);
try {
stream.read();
//inne operacje...

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

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

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

}
}
}
}
}