サンプル
public class
ClassA {
public void
makeStream (InputStream iStream) {
try
{
InputStreamReader stream =
new
InputStreamReader(iStream);
stream.read();
//何らかの処理...
stream.close();
}
catch
(IOException e) {
//ストリームのリーク発生...
}
}
}
ソリューション
リソース・リークを回避するには、すべての java.io.InputStreamReader で close() が呼び出されるようにします。
注: この規則は、あらゆる形式の InputStreamReader コンストラクターに適用されます。
public class
ClassA {
public void
makeStream (InputStream iStream) {
InputStreamReader stream =
null
;
try
{
stream =
new
InputStreamReader(iStream);
stream.read();
//何らかの処理...
}
catch
(IOException e) {
//何らかの処理...
}
finally
{
if
(stream !=
null
) {
try
{
stream.close();
}
catch
(IOException e) {
//何らかの処理...
}
}
}
}
}