private void sendStream(OutputStream os, InputStream is) throws IOException {
    int length;
    byte[] buffer = new byte[1024*8];
    if (getRequest().getHeader("Accept-Encoding") != null && getHeader("Content-Encoding") == null) {
      String encodings = getRequest().getHeader("Accept-Encoding").getValue();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      if (encodings.contains("gzip")) {
        setHeader("Content-Encoding", "gzip");
        GZIPOutputStream out = new GZIPOutputStream(bos);
        while ((length = is.read(buffer)) > 0) out.write(buffer, 0, length);
        out.finish();
        out.flush();
      } else if (encodings.contains("deflate")) {
        setHeader("Content-Encoding", "deflate");
        DeflaterOutputStream out = new DeflaterOutputStream(bos);
        while ((length = is.read(buffer)) > 0) out.write(buffer, 0, length);
        out.finish();
        out.flush();
      }
      setHeader("Content-Length", Integer.toString(bos.toByteArray().length));
      ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray());

      sendHeaders(os);
      BufferedOutputStream out = new BufferedOutputStream(os);
      while ((length = in.read(buffer)) > 0) out.write(buffer, 0, length);
      out.flush();
    } else {
      sendHeaders(os);
      BufferedOutputStream out = new BufferedOutputStream(os);
      while ((length = is.read(buffer)) > 0) out.write(buffer, 0, length);
      out.flush();
    }
  }