Class ByteArrayContent (1.44.2)

public final class ByteArrayContent extends AbstractInputStreamContent

Concrete implementation of AbstractInputStreamContent that generates repeatable input streams based on the contents of byte array.

Sample use:

 
 static void setJsonContent(HttpRequest request, byte[] json) {
 request.setContent(new ByteArrayContent("application/json", json));
 }
 
 

Implementation is not thread-safe.

Inheritance

java.lang.Object > AbstractInputStreamContent > ByteArrayContent

Static Methods

fromString(String type, String contentString)

public static ByteArrayContent fromString(String type, String contentString)

Returns a new instance with the UTF-8 encoding (using StringUtils#getBytesUtf8(String)) of the given content string.

Sample use:

 
 static void setJsonContent(HttpRequest request, String json) {
 request.setContent(ByteArrayContent.fromString("application/json", json));
 }
 
 
Parameters
Name Description
type String

content type or null for none

contentString String

content string

Returns
Type Description
ByteArrayContent

Constructors

ByteArrayContent(String type, byte[] array)

public ByteArrayContent(String type, byte[] array)

Constructor from byte array content that has already been encoded.

Parameters
Name Description
type String

content type or null for none

array byte[]

byte array content

ByteArrayContent(String type, byte[] array, int offset, int length)

public ByteArrayContent(String type, byte[] array, int offset, int length)

Constructor from byte array content that has already been encoded, specifying a range of bytes to read from the input byte array.

Parameters
Name Description
type String

content type or null for none

array byte[]

byte array content

offset int

starting offset into the byte array

length int

of bytes to read from byte array

Methods

getInputStream()

public InputStream getInputStream()

Return an input stream for the specific implementation type of AbstractInputStreamContent. If the specific implementation will return true for #retrySupported() this should be a factory function which will create a new InputStream from the source data whenever invoked.

Returns
Type Description
InputStream
Overrides

getLength()

public long getLength()
Returns
Type Description
long

retrySupported()

public boolean retrySupported()
Returns
Type Description
boolean

setCloseInputStream(boolean closeInputStream)

public ByteArrayContent setCloseInputStream(boolean closeInputStream)

Sets whether the input stream should be closed at the end of #writeTo. Default is true. Subclasses should override by calling super.

Parameter
Name Description
closeInputStream boolean
Returns
Type Description
ByteArrayContent
Overrides

setType(String type)

public ByteArrayContent setType(String type)

Sets the content type or null for none. Subclasses should override by calling super.

Parameter
Name Description
type String
Returns
Type Description
ByteArrayContent
Overrides