Skip to content

Commit

Permalink
Closing InputStream when image from tempFile is send to browser. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudy De Busscher committed Jun 27, 2015
1 parent 2f8e495 commit 763c0fd
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*/
public class ImageResourceHandler extends ResourceHandlerWrapper {

private static final Logger LOG = Logger.getLogger(ImageResourceHandler.class.getName());

private ResourceHandler wrapped;

public ImageResourceHandler(ResourceHandler original) {
Expand Down Expand Up @@ -64,10 +68,14 @@ public void handleResourceRequest(FacesContext context) throws IOException {

byte[] buffer = new byte[2048];

int length;
InputStream inputStream = streamedContent.getStream();
while ((length = (inputStream.read(buffer))) >= 0) {
externalContext.getResponseOutputStream().write(buffer, 0, length);
try {
int length;
InputStream inputStream = streamedContent.getStream();
while ((length = (inputStream.read(buffer))) >= 0) {
externalContext.getResponseOutputStream().write(buffer, 0, length);
}
} finally {
closeStreamContent(streamedContent);
}

externalContext.responseFlushBuffer();
Expand All @@ -78,5 +86,16 @@ public void handleResourceRequest(FacesContext context) throws IOException {
}

}

private void closeStreamContent(StreamedContent streamedContent) {
try {
if (streamedContent != null) {
streamedContent.getStream().close();
}
} catch (Exception e) {
LOG.log(Level.SEVERE, "Unexpected error took while attempting to close the streamed content of temporary file associated to the advanced graphic image renderer",
e);
}
}
}

0 comments on commit 763c0fd

Please sign in to comment.