jira-importer opened a new issue, #1295:
URL: https://github.com/apache/httpcomponents-core/issues/1295

   **[Bill 
Speirs](https://issues.apache.org/jira/secure/ViewProfile.jspa?name=wspeirs)** 
opened 
**[HTTPCORE-258](https://issues.apache.org/jira/browse/HTTPCORE-258?redirect=false)**
 and commented
   
   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();
   
     }
   
   
   
   ---
   
   **Attachments:**
   - 
[ByteArrayEntity.diff](https://issues.apache.org/jira/secure/attachment/12481259/ByteArrayEntity.diff)
 (_1.95 kB_)
   - 
[NByteArrayEntity.diff](https://issues.apache.org/jira/secure/attachment/12481260/NByteArrayEntity.diff)
 (_2.21 kB_)
   - 
[TestByteArrayEntity.diff](https://issues.apache.org/jira/secure/attachment/12481262/TestByteArrayEntity.diff)
 (_2.42 kB_)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to