You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by mi...@apache.org on 2021/11/13 18:18:55 UTC

[httpcomponents-client] branch master updated: HTTPCLIENT-2066 Provide ByteArrayBody constructors w/o filename parameter

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/master by this push:
     new af2cc82  HTTPCLIENT-2066 Provide ByteArrayBody constructors w/o filename parameter
af2cc82 is described below

commit af2cc82e823d696d874b561fa3d81c754b6fa04d
Author: Arturo Bernal <ar...@gmail.com>
AuthorDate: Sat Oct 30 10:49:49 2021 +0200

    HTTPCLIENT-2066 Provide ByteArrayBody constructors w/o filename parameter
    
    This closes #319
---
 .../apache/hc/client5/http/entity/mime/ByteArrayBody.java | 15 +++++++++++++--
 .../client5/http/entity/mime/MultipartEntityBuilder.java  |  2 +-
 .../http/entity/mime/TestMultipartEntityBuilder.java      |  4 +++-
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java
index 3fdb871..fba5eff 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/ByteArrayBody.java
@@ -56,12 +56,23 @@ public class ByteArrayBody extends AbstractContentBody {
      */
     public ByteArrayBody(final byte[] data, final ContentType contentType, final String filename) {
         super(contentType);
-        Args.notNull(data, "byte[]");
-        this.data = data;
+        this.data = Args.notNull(data, "data");
         this.filename = filename;
     }
 
     /**
+     * Public constructor that creates a new ByteArrayBody.
+     *
+     * @param contentType the {@link ContentType}
+     * @param data the array of byte.
+     *
+     * @since 5.2
+     */
+    public ByteArrayBody(final byte[] data, final ContentType contentType) {
+        this(data, contentType, null);
+    }
+
+    /**
      * Creates a new ByteArrayBody.
      *
      * @param data The contents of the file contained in this part.
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
index 5e55f40..543bbc7 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/MultipartEntityBuilder.java
@@ -155,7 +155,7 @@ public class MultipartEntityBuilder {
 
     public MultipartEntityBuilder addBinaryBody(
             final String name, final byte[] b) {
-        return addBinaryBody(name, b, ContentType.DEFAULT_BINARY, null);
+        return addPart(name, new ByteArrayBody(b, ContentType.DEFAULT_BINARY));
     }
 
     public MultipartEntityBuilder addBinaryBody(
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
index abaedb2..3bbd7b7 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/entity/mime/TestMultipartEntityBuilder.java
@@ -70,13 +70,15 @@ public class TestMultipartEntityBuilder {
                 .addBinaryBody("p2", new File("stuff"))
                 .addBinaryBody("p3", new byte[]{})
                 .addBinaryBody("p4", new ByteArrayInputStream(new byte[]{}))
+                .addBinaryBody("p5", new ByteArrayInputStream(new byte[]{}), ContentType.DEFAULT_BINARY, "filename")
                 .buildEntity();
         Assert.assertNotNull(entity);
         final List<MultipartPart> bodyParts = entity.getMultipart().getParts();
         Assert.assertNotNull(bodyParts);
-        Assert.assertEquals(4, bodyParts.size());
+        Assert.assertEquals(5, bodyParts.size());
     }
 
+
     @Test
     public void testMultipartCustomContentType() throws Exception {
         final MultipartFormEntity entity = MultipartEntityBuilder.create()