Class ByteArrayContent (1.41.8)

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
NameDescription
typeString

content type or null for none

contentStringString

content string

Returns
TypeDescription
ByteArrayContent

Constructors

ByteArrayContent(String type, byte[] array)

public ByteArrayContent(String type, byte[] array)

Constructor from byte array content that has already been encoded.

Parameters
NameDescription
typeString

content type or null for none

arraybyte[]

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
NameDescription
typeString

content type or null for none

arraybyte[]

byte array content

offsetint

starting offset into the byte array

lengthint

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
TypeDescription
InputStream
Overrides

getLength()

public long getLength()
Returns
TypeDescription
long

retrySupported()

public boolean retrySupported()
Returns
TypeDescription
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
NameDescription
closeInputStreamboolean
Returns
TypeDescription
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
NameDescription
typeString
Returns
TypeDescription
ByteArrayContent
Overrides