HTTPCLIENT-2066 Provide ByteArrayBody constructors w/o filename parameter

This closes #319
This commit is contained in:
Arturo Bernal 2021-10-30 10:49:49 +02:00 committed by Michael Osipov
parent 0926f1e07a
commit af2cc82e82
3 changed files with 17 additions and 4 deletions

View File

@ -56,11 +56,22 @@ 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.
*

View File

@ -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(

View File

@ -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()