Class ByteArrayContent
java.lang.Object
com.google.api.client.http.AbstractInputStreamContent
com.google.api.client.http.ByteArrayContent
- All Implemented Interfaces:
HttpContent, StreamingContent
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.
- Since:
- 1.4
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionByteArrayContent(String type, byte[] array) Constructor from byte array content that has already been encoded.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. -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteArrayContentfromString(String type, String contentString) Returns a new instance with the UTF-8 encoding (usingStringUtils.getBytesUtf8(String)) of the given content string.Return an input stream for the specific implementation type ofAbstractInputStreamContent.longReturns the content length or less than zero if not known.booleanReturns whether or not retry is supported on this content type.setCloseInputStream(boolean closeInputStream) Sets whether the input stream should be closed at the end ofAbstractInputStreamContent.writeTo(OutputStream).Sets the content type ornullfor none.Methods inherited from class AbstractInputStreamContent
getCloseInputStream, getType, writeTo
-
Field Details
-
byteArray
private final byte[] byteArrayByte array content. -
offset
private final int offsetStarting offset into the byte array. -
length
private final int lengthLength of bytes to read from byte array.
-
-
Constructor Details
-
ByteArrayContent
Constructor from byte array content that has already been encoded.- Parameters:
type- content type ornullfor nonearray- byte array content- Since:
- 1.5
-
ByteArrayContent
Constructor from byte array content that has already been encoded, specifying a range of bytes to read from the input byte array.- Parameters:
type- content type ornullfor nonearray- byte array contentoffset- starting offset into the byte arraylength- of bytes to read from byte array- Since:
- 1.7
-
-
Method Details
-
fromString
Returns a new instance with the UTF-8 encoding (usingStringUtils.getBytesUtf8(String)) of the given content string.Sample use:
static void setJsonContent(HttpRequest request, String json) { request.setContent(ByteArrayContent.fromString("application/json", json)); }- Parameters:
type- content type ornullfor nonecontentString- content string- Since:
- 1.5
-
getLength
public long getLength()Description copied from interface:HttpContentReturns the content length or less than zero if not known. -
retrySupported
public boolean retrySupported()Description copied from interface:HttpContentReturns whether or not retry is supported on this content type. -
getInputStream
Description copied from class:AbstractInputStreamContentReturn an input stream for the specific implementation type ofAbstractInputStreamContent. If the specific implementation will returntrueforHttpContent.retrySupported()this should be a factory function which will create a newInputStreamfrom the source data whenever invoked.- Specified by:
getInputStreamin classAbstractInputStreamContent
-
setType
Description copied from class:AbstractInputStreamContentSets the content type ornullfor none. Subclasses should override by calling super.- Overrides:
setTypein classAbstractInputStreamContent
-
setCloseInputStream
Description copied from class:AbstractInputStreamContentSets whether the input stream should be closed at the end ofAbstractInputStreamContent.writeTo(OutputStream). Default istrue. Subclasses should override by calling super.- Overrides:
setCloseInputStreamin classAbstractInputStreamContent
-