Package com.google.api.client.http
Class ByteArrayContent
- java.lang.Object
-
- com.google.api.client.http.AbstractInputStreamContent
-
- com.google.api.client.http.ByteArrayContent
-
- All Implemented Interfaces:
HttpContent
,StreamingContent
public final class ByteArrayContent extends AbstractInputStreamContent
Concrete implementation ofAbstractInputStreamContent
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
-
-
Constructor Summary
Constructors Constructor Description ByteArrayContent(java.lang.String type, byte[] array)
Constructor from byte array content that has already been encoded.ByteArrayContent(java.lang.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ByteArrayContent
fromString(java.lang.String type, java.lang.String contentString)
Returns a new instance with the UTF-8 encoding (usingStringUtils.getBytesUtf8(String)
) of the given content string.java.io.InputStream
getInputStream()
Return an input stream for the specific implementation type ofAbstractInputStreamContent
.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 ofAbstractInputStreamContent.writeTo(java.io.OutputStream)
.ByteArrayContent
setType(java.lang.String type)
Sets the content type ornull
for none.-
Methods inherited from class com.google.api.client.http.AbstractInputStreamContent
getCloseInputStream, getType, writeTo
-
-
-
-
Constructor Detail
-
ByteArrayContent
public ByteArrayContent(java.lang.String type, byte[] array)
Constructor from byte array content that has already been encoded.- Parameters:
type
- content type ornull
for nonearray
- byte array content- Since:
- 1.5
-
ByteArrayContent
public ByteArrayContent(java.lang.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:
type
- content type ornull
for nonearray
- byte array contentoffset
- starting offset into the byte arraylength
- of bytes to read from byte array- Since:
- 1.7
-
-
Method Detail
-
fromString
public static ByteArrayContent fromString(java.lang.String type, java.lang.String contentString)
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 ornull
for nonecontentString
- content string- Since:
- 1.5
-
getLength
public long getLength()
Description copied from interface:HttpContent
Returns the content length or less than zero if not known.
-
retrySupported
public boolean retrySupported()
Description copied from interface:HttpContent
Returns whether or not retry is supported on this content type.
-
getInputStream
public java.io.InputStream getInputStream()
Description copied from class:AbstractInputStreamContent
Return an input stream for the specific implementation type ofAbstractInputStreamContent
. If the specific implementation will returntrue
forHttpContent.retrySupported()
this should be a factory function which will create a newInputStream
from the source data whenever invoked.- Specified by:
getInputStream
in classAbstractInputStreamContent
-
setType
public ByteArrayContent setType(java.lang.String type)
Description copied from class:AbstractInputStreamContent
Sets the content type ornull
for none. Subclasses should override by calling super.- Overrides:
setType
in classAbstractInputStreamContent
-
setCloseInputStream
public ByteArrayContent setCloseInputStream(boolean closeInputStream)
Description copied from class:AbstractInputStreamContent
Sets whether the input stream should be closed at the end ofAbstractInputStreamContent.writeTo(java.io.OutputStream)
. Default istrue
. Subclasses should override by calling super.- Overrides:
setCloseInputStream
in classAbstractInputStreamContent
-
-