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