public int doWrite(ByteChunk chunk, Response res) throws IOException {
    if (!res.isCommitted()) {
      // Send the connector a request for commit. The connector should
      // then validate the headers, send them (using sendHeader) and
      // set the filters accordingly.
      res.sendHeaders();
    }

    int len = chunk.getLength();
    byte buf[] = outputMsg.getBuffer();
    // 4 - hardcoded, byte[] marshalling overhead
    int chunkSize = buf.length - outputMsg.getHeaderLength() - 4;
    int off = 0;
    while (len > 0) {
      int thisTime = len;
      if (thisTime > chunkSize) {
        thisTime = chunkSize;
      }
      len -= thisTime;

      outputMsg.reset();
      outputMsg.appendByte(AjpConstants.JK_AJP13_SEND_BODY_CHUNK);
      if (log.isTraceEnabled()) log.trace("doWrite " + off + " " + thisTime + " " + len);
      outputMsg.appendBytes(chunk.getBytes(), chunk.getOffset() + off, thisTime);
      off += thisTime;
      mc.getSource().send(outputMsg, mc);
    }
    return 0;
  }