Skip to main content

JAX-RS - Returning a Response containing an InputStream. Who closes it?

No replies
michaelgoossens
Offline
Joined: 2010-12-20
Points: 0

Hello,

I wrote a get method that returns an inputstream. This all works but I never call the close() method on the stream. I can't call the close() operation because then the data isn't received on the client. Fortify scan's (a code quality tool) has a problem with this however and thus also my superiors.

So I was wondering if this is ok, to return an inputstream like this (I don't know another way and I find similar code on the internet), or should I do this another way (this is actually part of a WEBDEV implementation that sends a file to the client)?
I suspect that some JAX-RS component closes the inputstream afterwards but I don't have any proof for this, is this the case and where can I find it in the documentation if it is?

@GET
@Produces("application/octet-stream")
public javax.ws.rs.core.Response get() {
if (!resource.exists()) {
return javax.ws.rs.core.Response.status(404).build();
} else {
ResponseBuilder builder = javax.ws.rs.core.Response.ok();
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream(resource), BUFFER_SIZE);
} catch (FileNotFoundException e) {
return javax.ws.rs.core.Response.serverError().build();
}
builder.header("Last-Modified", new Rfc1123DateFormat().format(new Date(resource.lastModified())));
builder.header("Content-Length", resource.length());

return builder.entity(in).build();
}
}