Java/Fortify - Unreleased resource:Stream

Viewed 551
try (java.io.ByteArrayOutputStream out = (java.io.ByteArrayOutputStream) this.httpConnect.getOutputStream();) {

For this line fortify is giving - Unreleased resource:Stream Vulnerability. Any reason? We have already added it into try with resorces.

Do I need to use the old pattern [try.catch/finally] to close the resource?

Here, this.httpConnect is : HttpURLConnection httpConnect = null;

Please provide some suggestions here.

1 Answers

Presumably if getOutputStream() returns something other than a ByteArrayOutputStream (or null) then the cast will throw. The resource will not reach the try so cannot be closed.

So, avoid doing the cast in the try-with-resource statement expression. Or preferably, avoid casts altogether.

Related