You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by bu...@apache.org on 2005/09/14 06:33:27 UTC

DO NOT REPLY [Bug 36647] New: - RequestEntity wrapping a ByteBuffer

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=36647>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=36647

           Summary: RequestEntity wrapping a ByteBuffer
           Product: HttpClient
           Version: 3.0 RC3
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: Commons HttpClient
        AssignedTo: httpclient-dev@jakarta.apache.org
        ReportedBy: sormuras@gmx.de


Good morning everybody,

here's a little code contribution which might be useful for others.

Cheers,
Christian

-- 

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import org.apache.commons.httpclient.methods.RequestEntity;

public class ByteBufferRequestEntity implements RequestEntity {

  private static final int WRITE_CHUNK_SIZE = 1024;

  private final ByteBuffer content;
  private final String contentType;

  public ByteBufferRequestEntity(ByteBuffer source) {
    this(source, null);
  }

  public ByteBufferRequestEntity(ByteBuffer content, String contentType) {
    if (content == null) {
      throw new IllegalArgumentException("The content cannot be null");
    }
    this.content = content;
    this.contentType = contentType;
  }

  public ByteBuffer getContent() {
    return content.asReadOnlyBuffer();
  }

  public long getContentLength() {
    return content.remaining();
  }

  public String getContentType() {
    return contentType;
  }

  public boolean isRepeatable() {
    return true;
  }

  public void writeRequest(OutputStream out) throws IOException {
    ByteBuffer buffer = content.asReadOnlyBuffer();
    // Trivial implementation:
    // .. while (buffer.hasRemaining()) {
    // .... out.write(buffer.get());
    // .. }
    byte[] chunk = null;
    if (buffer.remaining() >= WRITE_CHUNK_SIZE) {
      chunk = new byte[WRITE_CHUNK_SIZE];
      while (buffer.remaining() >= WRITE_CHUNK_SIZE) {
        buffer.get(chunk, 0, WRITE_CHUNK_SIZE);
        out.write(chunk, 0, WRITE_CHUNK_SIZE);
      }
    }
    int remaining = buffer.remaining();
    if (remaining > 0) {
      chunk = new byte[remaining];
      buffer.get(chunk, 0, remaining);
      out.write(chunk, 0, remaining);
    }
  }

}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org