You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Sebb (JIRA)" <ji...@apache.org> on 2011/06/02 18:29:47 UTC
[jira] [Commented] (HTTPCORE-258) Add Offset and Length to
ByteArrayEntity
[ https://issues.apache.org/jira/browse/HTTPCORE-258?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13042849#comment-13042849 ]
Sebb commented on HTTPCORE-258:
-------------------------------
Should off and len be final? Does it make sense to change them? Could they be private?
> Add Offset and Length to ByteArrayEntity
> ----------------------------------------
>
> Key: HTTPCORE-258
> URL: https://issues.apache.org/jira/browse/HTTPCORE-258
> Project: HttpComponents HttpCore
> Issue Type: Improvement
> Components: HttpCore, HttpCore NIO
> Environment: N/A
> Reporter: William R. Speirs
> Priority: Trivial
> Fix For: 4.2-alpha1
>
>
> Currently the ByteArrayEntity only has a single constructor which will only accept a byte[]. I suggest this class be extended to handle an additional constructor which takes an offset and length. I have created the following first draft of a patch to the ByteArrayEntity class. Additional changes will need to be made to the test cases and the NByteArrayEntity class as well.
> --- httpcore/src/main/java/org/apache/http/entity/ByteArrayEntity.java (revision 1130603)
> +++ httpcore/src/main/java/org/apache/http/entity/ByteArrayEntity.java (working copy)
> @@ -40,6 +40,7 @@
> public class ByteArrayEntity extends AbstractHttpEntity implements Cloneable {
> protected final byte[] content;
> + protected int off, len;
> public ByteArrayEntity(final byte[] b) {
> super();
> @@ -47,25 +48,43 @@
> throw new IllegalArgumentException("Source byte array may not be null");
> }
> this.content = b;
> + this.off = 0;
> + this.len = this.content.length;
> }
> + public ByteArrayEntity(final byte[] b, int off, int len) {
> + super();
> + if (b == null) {
> + throw new IllegalArgumentException("Source byte array may not be null");
> + }
> + if (off < 0) {
> + throw new IllegalArgumentException("Offset cannot be negative");
> + }
> + if(b.length - off >= len) {
> + throw new IllegalArgumentException("Length cannot be longer than byte array");
> + }
> + this.content = b;
> + this.off = off;
> + this.len = len;
> + }
> +
> public boolean isRepeatable() {
> return true;
> }
> public long getContentLength() {
> - return this.content.length;
> + return this.len - this.off;
> }
> public InputStream getContent() {
> - return new ByteArrayInputStream(this.content);
> + return new ByteArrayInputStream(this.content, this.off, this.len);
> }
> public void writeTo(final OutputStream outstream) throws IOException {
> if (outstream == null) {
> throw new IllegalArgumentException("Output stream may not be null");
> }
> - outstream.write(this.content);
> + outstream.write(this.content, this.off, this.len);
> outstream.flush();
> }
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org