title: Processing an InputStream to OutputStream on the fly through a HttpServlet
body: <p>I'm trying to process a large text file through a HttpServlet (tomcat).</p>
  
  <p>As this file can be large and the process should be very fast, I don't want to upload the file on the server and I've used the method <code>HttpRequest.getInputStream</code> to process the input on the fly. For example, I want to transform the input to upper-case with the code below:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  @SuppressWarnings("serial")
  public class EchoServlet extends HttpServlet
      {
      @Override
      protected void doPost(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException
          {
          OutputStream out=null;
          InputStream in=req.getInputStream();
          if(in==null) throw new IOException("input is null");
          try
              {
              resp.setContentType("text/plain");
              out=resp.getOutputStream();
              int c;
              while((c=in.read())!=-1)
                  {
                  out.write((char)Character.toUpperCase(c));
                  }
              }
          catch(IOException err)
              {
              //ignore
              }
          finally
              {
              if(out!=null) out.flush();
              if(out!=null) out.close();
              in.close();
              }
          }
      }
  </code></pre>
  
  <p>I invoked my servlet with CURL:</p>
  
  <pre><code>curl -s -d @big.file.txt  "http://localhost:8080/test/toupper"
  </code></pre>
  
  <p>1) processing the input on the fly through a servlet, is it a good/common practice ?</p>
  
  <p>2) my code seems to remove the carriage return ('\n') . Why ?</p>
  
  <p>Thanks</p>