Package com.google.api.client.http
Class AbstractHttpContent
java.lang.Object
com.google.api.client.http.AbstractHttpContent
- All Implemented Interfaces:
HttpContent
,StreamingContent
- Direct Known Subclasses:
AbstractXmlHttpContent
,JsonHttpContent
,MultipartContent
,ProtoHttpContent
,UrlEncodedContent
Abstract implementation of an HTTP content with typical options.
Implementation is not thread-safe.
- Since:
- 1.5
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate long
Cached value for the computed length fromcomputeLength()
.private HttpMediaType
Media type used for the Content-Type header ornull
for none. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractHttpContent
(HttpMediaType mediaType) protected
AbstractHttpContent
(String mediaType) -
Method Summary
Modifier and TypeMethodDescriptionprotected long
Computes and returns the content length or less than zero if not known.static long
computeLength
(HttpContent content) Returns the computed content length based usingIOUtils.computeLength(StreamingContent)
or instead-1
ifHttpContent.retrySupported()
isfalse
because the stream must not be read twice.protected final Charset
Returns the charset specified in the media type orCharsets#UTF_8
if not specified.long
Default implementation callscomputeLength()
once and caches it for future invocations, but subclasses may override.final HttpMediaType
Returns the media type to use for the Content-Type header, ornull
if unspecified.getType()
Returns the content type ornull
for none.boolean
Default implementation returnstrue
, but subclasses may override.setMediaType
(HttpMediaType mediaType) Sets the media type to use for the Content-Type header, ornull
if unspecified.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.google.api.client.http.HttpContent
writeTo
-
Field Details
-
mediaType
Media type used for the Content-Type header ornull
for none. -
computedLength
private long computedLengthCached value for the computed length fromcomputeLength()
.
-
-
Constructor Details
-
AbstractHttpContent
- Parameters:
mediaType
- Media type string (for example "type/subtype") this content represents ornull
to leave out. Can also contain parameters like"charset=utf-8"
- Since:
- 1.10
-
AbstractHttpContent
- Parameters:
mediaType
- Media type this content represents ornull
to leave out- Since:
- 1.10
-
-
Method Details
-
getLength
Default implementation callscomputeLength()
once and caches it for future invocations, but subclasses may override.- Specified by:
getLength
in interfaceHttpContent
- Throws:
IOException
-
getMediaType
Returns the media type to use for the Content-Type header, ornull
if unspecified.- Since:
- 1.10
-
setMediaType
Sets the media type to use for the Content-Type header, ornull
if unspecified.This will also overwrite any previously set parameter of the media type (for example
"charset"
), and therefore might change other properties as well.- Since:
- 1.10
-
getCharset
Returns the charset specified in the media type orCharsets#UTF_8
if not specified.- Since:
- 1.10
-
getType
Description copied from interface:HttpContent
Returns the content type ornull
for none.- Specified by:
getType
in interfaceHttpContent
-
computeLength
Computes and returns the content length or less than zero if not known.Subclasses may override, but by default this computes the length by calling
computeLength(HttpContent)
.- Throws:
IOException
-
retrySupported
public boolean retrySupported()Default implementation returnstrue
, but subclasses may override.- Specified by:
retrySupported
in interfaceHttpContent
-
computeLength
Returns the computed content length based usingIOUtils.computeLength(StreamingContent)
or instead-1
ifHttpContent.retrySupported()
isfalse
because the stream must not be read twice.- Parameters:
content
- HTTP content- Returns:
- computed content length or
-1
if retry is not supported - Throws:
IOException
- Since:
- 1.14
-