Przykład

public class ClassA {

public void makeStream (InputStream iStream) {

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


public class ClassA {

public void makeStream (InputStream iStream) {

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

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

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

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

}
}
}
}
}