@Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {

      //Mimic download of the package tar-ball
      response.setContentType("application/gzip");
      response.setStatus(HttpServletResponse.SC_OK);

      try {
        int currByteWritten = -1;
        int numBytesWritten = 0;
        //Begin streaming a response.
        InputStream in = new ByteArrayInputStream(RESPONSE_STR.getBytes());
        OutputStream out = response.getOutputStream();

        while ((currByteWritten = in.read()) != -1) {
          out.write(currByteWritten);
          out.flush();
          numBytesWritten++;

          //Hang the connection until the read timeout expires on the client side.
          if (numBytesWritten >= THRESHOLD_BYTES) {
            if (!serverWaitLatch.await(5, TimeUnit.SECONDS)) {
              throw new IOException("Timed out waiting for latch");
            }
            break;
          }
        }
      } catch (Exception e) {
        //Record any exception that may have occurred
        LOG.error("{}", e);
        serverException = e;
      }
    }
A {@link HttpServlet} implementation that streams its response to the client one byte at a time.
samza-yarn/src/test/java/org/apache/samza/job/yarn/util/hadoop/TestHttpFileSystem.java