Skip to main content

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

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2010-12-20


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?

public get() {
if (!resource.exists()) {
} else {
ResponseBuilder builder =;
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream(resource), BUFFER_SIZE);
} catch (FileNotFoundException e) {
builder.header("Last-Modified", new Rfc1123DateFormat().format(new Date(resource.lastModified())));
builder.header("Content-Length", resource.length());

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