public final class ByteArrayContent extends AbstractInputStreamContent
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.
| Constructor and Description |
|---|
ByteArrayContent(String
Constructor from byte array content that has already been encoded.
|
ByteArrayContent(String
Constructor from byte array content that has already been encoded, specifying a range of bytes to read from the input byte array.
|
| Modifier and Type | Method and Description |
|---|---|
static ByteArrayContent |
fromString(String
Returns a new instance with the UTF-8 encoding (using
StringUtils) of the given content string.
|
InputStream |
getInputStream()
Return an input stream for the specific implementation type of
AbstractInputStreamContent.
|
long |
getLength()
Returns the content length or less than zero if not known.
|
boolean |
retrySupported()
Returns whether or not retry is supported on this content type.
|
ByteArrayContent |
setCloseInputStream(boolean closeInputStream)
Sets whether the input stream should be closed at the end of
AbstractInputStreamContent.
|
ByteArrayContent |
setType(String
Sets the content type or
null for none.
|
getCloseInputStream, getType, writeTopublic ByteArrayContent(Stringtype, byte[] array)
type - content type or
null for none
array - byte array content
public ByteArrayContent(Stringtype, byte[] array, int offset, int length)
type - content type or
null for none
array - byte array content
offset - starting offset into the byte array
length - of bytes to read from byte array
public static ByteArrayContentfromString(String type, String contentString)
StringUtils.getBytesUtf8(String) ) of the given content string.
Sample use:
static void setJsonContent(HttpRequest request, String json) {
request.setContent(ByteArrayContent.fromString("application/json", json));
}
type - content type or
null for none
contentString - content string
public long getLength()
HttpContent
public boolean retrySupported()
HttpContent
public InputStreamgetInputStream()
AbstractInputStreamContent
AbstractInputStreamContent. If the specific implementation will return
true for
HttpContent.retrySupported() this should be a factory function which will create a new
InputStream from the source data whenever invoked.
public ByteArrayContentsetType(String type)
AbstractInputStreamContent
null for none. Subclasses should override by calling super.
public ByteArrayContentsetCloseInputStream(boolean closeInputStream)
AbstractInputStreamContent
AbstractInputStreamContent.writeTo(java.io.OutputStream) . Default is
true. Subclasses should override by calling super.