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
public abstract class AbstractHttpContent extends java.lang.Object implements HttpContent
Abstract implementation of an HTTP content with typical options.Implementation is not thread-safe.
- Since:
- 1.5
-
-
Field Summary
Fields Modifier and Type Field Description private longcomputedLengthCached value for the computed length fromcomputeLength().private HttpMediaTypemediaTypeMedia type used for the Content-Type header ornullfor none.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractHttpContent(HttpMediaType mediaType)protectedAbstractHttpContent(java.lang.String mediaType)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected longcomputeLength()Computes and returns the content length or less than zero if not known.static longcomputeLength(HttpContent content)Returns the computed content length based usingIOUtils.computeLength(StreamingContent)or instead-1ifHttpContent.retrySupported()isfalsebecause the stream must not be read twice.protected java.nio.charset.CharsetgetCharset()Returns the charset specified in the media type or ISO_8859_1 if not specified.longgetLength()Default implementation callscomputeLength()once and caches it for future invocations, but subclasses may override.HttpMediaTypegetMediaType()Returns the media type to use for the Content-Type header, ornullif unspecified.java.lang.StringgetType()Returns the content type ornullfor none.booleanretrySupported()Default implementation returnstrue, but subclasses may override.AbstractHttpContentsetMediaType(HttpMediaType mediaType)Sets the media type to use for the Content-Type header, ornullif 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 Detail
-
mediaType
private HttpMediaType mediaType
Media type used for the Content-Type header ornullfor none.
-
computedLength
private long computedLength
Cached value for the computed length fromcomputeLength().
-
-
Constructor Detail
-
AbstractHttpContent
protected AbstractHttpContent(java.lang.String mediaType)
- Parameters:
mediaType- Media type string (for example "type/subtype") this content represents ornullto leave out. Can also contain parameters like"charset=utf-8"- Since:
- 1.10
-
AbstractHttpContent
protected AbstractHttpContent(HttpMediaType mediaType)
- Parameters:
mediaType- Media type this content represents ornullto leave out- Since:
- 1.10
-
-
Method Detail
-
getLength
public long getLength() throws java.io.IOExceptionDefault implementation callscomputeLength()once and caches it for future invocations, but subclasses may override.- Specified by:
getLengthin interfaceHttpContent- Throws:
java.io.IOException
-
getMediaType
public final HttpMediaType getMediaType()
Returns the media type to use for the Content-Type header, ornullif unspecified.- Since:
- 1.10
-
setMediaType
public AbstractHttpContent setMediaType(HttpMediaType mediaType)
Sets the media type to use for the Content-Type header, ornullif 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
protected final java.nio.charset.Charset getCharset()
Returns the charset specified in the media type or ISO_8859_1 if not specified.- Since:
- 1.10
-
getType
public java.lang.String getType()
Description copied from interface:HttpContentReturns the content type ornullfor none.- Specified by:
getTypein interfaceHttpContent
-
computeLength
protected long computeLength() throws java.io.IOExceptionComputes 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:
java.io.IOException
-
retrySupported
public boolean retrySupported()
Default implementation returnstrue, but subclasses may override.- Specified by:
retrySupportedin interfaceHttpContent
-
computeLength
public static long computeLength(HttpContent content) throws java.io.IOException
Returns the computed content length based usingIOUtils.computeLength(StreamingContent)or instead-1ifHttpContent.retrySupported()isfalsebecause the stream must not be read twice.- Parameters:
content- HTTP content- Returns:
- computed content length or
-1if retry is not supported - Throws:
java.io.IOException- Since:
- 1.14
-
-