From 1565559df856304c7d6ad8457c5753e3c2840daf Mon Sep 17 00:00:00 2001 From: luc Date: Fri, 20 Nov 2015 09:42:24 +0100 Subject: [PATCH] Refactoring : extracted write InputStream method. --- .../http/servlets/YaCyDefaultServlet.java | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/source/net/yacy/http/servlets/YaCyDefaultServlet.java b/source/net/yacy/http/servlets/YaCyDefaultServlet.java index 78ddaea41..5ad95e9d0 100644 --- a/source/net/yacy/http/servlets/YaCyDefaultServlet.java +++ b/source/net/yacy/http/servlets/YaCyDefaultServlet.java @@ -882,18 +882,7 @@ public class YaCyDefaultServlet extends HttpServlet { } if (tmp instanceof InputStream) { - final InputStream is = (InputStream) tmp; - final String mimeType = Classification.ext2mime(targetExt, TEXT_HTML.asString()); - response.setContentType(mimeType); - response.setStatus(HttpServletResponse.SC_OK); - byte[] buffer = new byte[4096]; - int l, size = 0; - try { - while ((l = is.read(buffer)) > 0) {response.getOutputStream().write(buffer, 0, l); size += l;} - response.setContentLength(size); - } finally { - is.close(); - } + writeInputStream(response, targetExt, (InputStream)tmp); return; } @@ -993,6 +982,35 @@ public class YaCyDefaultServlet extends HttpServlet { } } } + + + /** + * Write input stream content to response and close input stream. + * @param response servlet response. Must not be null. + * @param targetExt response file format + * @param tmp + * @throws IOException when a read/write error occured. + */ + private void writeInputStream(HttpServletResponse response, String targetExt, InputStream inStream) + throws IOException { + final String mimeType = Classification.ext2mime(targetExt, TEXT_HTML.asString()); + response.setContentType(mimeType); + response.setStatus(HttpServletResponse.SC_OK); + byte[] buffer = new byte[4096]; + int l, size = 0; + try { + while ((l = inStream.read(buffer)) > 0) { + response.getOutputStream().write(buffer, 0, l); + size += l; + } + response.setContentLength(size); + } finally { + try { + inStream.close(); + } catch(IOException ignored) { + } + } + } private static String appendPath(String proplist, String path) { if (proplist.length() == 0) return path;