/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 */
package org.ocelotds.web;

import org.ocelotds.Constants;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet to serve ocelot.js
 *
 * @author hhfrancois
 */
@WebServlet(urlPatterns = {Constants.SLASH_OCELOT_JS})
public class JSServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1973549844535787671L;

	private final static Logger logger = LoggerFactory.getLogger(JSServlet.class);

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType(Constants.JSTYPE);
		String minify = request.getParameter(Constants.MINIFY_PARAMETER);
		String filename = request.getServletContext().getInitParameter(Constants.OCELOT_MIN);
		if (Constants.FALSE.equalsIgnoreCase(minify)) {
			filename = request.getServletContext().getInitParameter(Constants.OCELOT);
		}
		long lg = new File(filename).length();
		logger.debug("Get resource {}({})", filename, lg);
		response.setContentLength((int) lg);
		try (Writer out = response.getWriter()) {
			try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
				String inputLine;
				while ((inputLine = in.readLine()) != null) {
					out.write(inputLine);
					if(in.ready())	out.write(Constants.BACKSLASH_N);
				}
			}
		}
	}

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP <code>GET</code> method.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			  throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			  throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 *
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "ocelot.js";
	}// </editor-fold>

}

@@ -280,30 +280,20 @@
 java.io.
-BufferedReader
+File
 ;%0D%0Aimpor
@@ -298,32 +298,43 @@
 ort java.io.File
+InputStream
 ;%0D%0Aimport java.i
@@ -335,26 +335,27 @@
 java.io.
-FileReader
+IOException
 ;%0D%0Aimpor
@@ -361,34 +361,34 @@
 rt java.io.I
-OException
+nputStream
 ;%0D%0Aimport ja
@@ -393,22 +393,28 @@
 java.io.
-Writer
+OutputStream
 ;%0D%0Aimpor
@@ -859,17 +859,16 @@
 vlet %7B%0D%0A
-%09
 %0D%0A%09priva
@@ -928,24 +928,83 @@
 787671L;%0D%0A%0D%0A
+%09private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;%0D%0A
 %09private fin
@@ -1926,24 +1926,43 @@
 me, lg);%0D%0A%09%09
+OutputStream out = 
 response.set
@@ -1962,143 +1962,89 @@
 nse.
-setContentLength((int) lg);%0D%0A%09%09try (Writer 
+getOutputStream();%0D%0A%09%09int c
 ou
+n
 t = 
-response.getWriter()) %7B
+0;
 %0D%0A
-%09
 %09%09try (
-BufferedReader in = new BufferedReader(new FileReader
+InputStream in = new FileInputStream
 (fil
@@ -2054,37 +2054,78 @@
 me))
-)
  %7B%0D%0A%09%09%09
-%09String inputLine
+byte%5B%5D buffer = new byte%5BDEFAULT_BUFFER_SIZE%5D;%0D%0A%09%09%09int n = 0
 ;%0D%0A
-%09
 %09%09%09w
@@ -2134,49 +2134,40 @@
 le (
-(inputLine = in.readLine()) != null
+-1 != (n = in.read(buffer))
 ) %7B%0D%0A
-%09
 %09%09%09%09
@@ -2180,25 +2180,28 @@
 ite(
-inputLine
+buffer, 0, n
 );%0D%0A%09%09%09%09
 %09if(
@@ -2200,68 +2200,29 @@
 %09%09%09%09
-%09if(in.ready())%09out.write(Constants.BACKSLASH_N)
+count += n
 ;%0D%0A
-%09
 %09%09%09%7D%0D%0A%09%09
 %09%7D%0D%0A
@@ -2221,15 +2221,46 @@
 %0D%0A%09%09
-%09
 %7D%0D%0A%09%09
-%7D
+response.setContentLength(count);
 %0D%0A%09%7D

improve stream ocelot.js