Exemplo n.º 1
0
  private boolean post(
      HTTPResponse httpRes,
      InputStream in,
      long contentOffset,
      long contentLength,
      boolean isOnlyHeader) {
    httpRes.setDate(Calendar.getInstance());
    OutputStream out = getOutputStream();

    try {
      httpRes.setContentLength(contentLength);

      out.write(httpRes.getHeader().getBytes());
      out.write(HTTP.CRLF.getBytes());

      if (isOnlyHeader == true) {
        out.flush();
        return true;
      }

      boolean isChunkedResponse = httpRes.isChunked();

      if (0 < contentOffset) in.skip(contentOffset);

      int chunkSize = HTTP.getChunkSize();
      byte readBuf[] = new byte[chunkSize];
      long readCnt = 0;
      long readSize = (chunkSize < contentLength) ? chunkSize : contentLength;
      int readLen = in.read(readBuf, 0, (int) readSize);
      while (0 < readLen && readCnt < contentLength) {
        if (isChunkedResponse == true) {
          String chunSizeBuf = Long.toString(readLen);
          out.write(chunSizeBuf.getBytes());
          out.write(HTTP.CRLF.getBytes());
        }
        out.write(readBuf, 0, readLen);
        if (isChunkedResponse == true) out.write(HTTP.CRLF.getBytes());
        readCnt += readLen;
        readSize = (chunkSize < (contentLength - readCnt)) ? chunkSize : (contentLength - readCnt);
        readLen = in.read(readBuf, 0, (int) readSize);
      }

      if (isChunkedResponse == true) {
        out.write("0".getBytes());
        out.write(HTTP.CRLF.getBytes());
      }

      out.flush();
    } catch (Exception e) {
      // Debug.warning(e);
      return false;
    }
    return true;
  }
Exemplo n.º 2
0
  private boolean post(
      HTTPResponse httpRes,
      byte content[],
      long contentOffset,
      long contentLength,
      boolean isOnlyHeader) {
    httpRes.setDate(Calendar.getInstance());
    OutputStream out = getOutputStream();

    try {
      httpRes.setContentLength(contentLength);

      out.write(httpRes.getHeader().getBytes());
      out.write(HTTP.CRLF.getBytes());
      if (isOnlyHeader == true) {
        out.flush();
        return true;
      }

      boolean isChunkedResponse = httpRes.isChunked();

      if (isChunkedResponse == true) {
        String chunSizeBuf = Long.toString(contentLength);
        out.write(chunSizeBuf.getBytes());
        out.write(HTTP.CRLF.getBytes());
      }

      out.write(content, (int) contentOffset, (int) contentLength);

      if (isChunkedResponse == true) {
        out.write(HTTP.CRLF.getBytes());
        out.write("0".getBytes());
        out.write(HTTP.CRLF.getBytes());
      }

      out.flush();
    } catch (Exception e) {
      // Debug.warning(e);
      return false;
    }

    return true;
  }