Class AbstractHttpContent (1.43.0)

public abstract class AbstractHttpContent implements HttpContent

Abstract implementation of an HTTP content with typical options.

Implementation is not thread-safe.

Inheritance

java.lang.Object > AbstractHttpContent

Implements

HttpContent

Static Methods

computeLength(HttpContent content)

public static long computeLength(HttpContent content)

Returns the computed content length based using IOUtils#computeLength(StreamingContent) or instead -1 if HttpContent#retrySupported() is false because the stream must not be read twice.

Parameter
NameDescription
contentHttpContent

HTTP content

Returns
TypeDescription
long

computed content length or -1 if retry is not supported

Exceptions
TypeDescription
IOException

Constructors

AbstractHttpContent(HttpMediaType mediaType)

protected AbstractHttpContent(HttpMediaType mediaType)
Parameter
NameDescription
mediaTypeHttpMediaType

Media type this content represents or null to leave out

AbstractHttpContent(String mediaType)

protected AbstractHttpContent(String mediaType)
Parameter
NameDescription
mediaTypeString

Media type string (for example "type/subtype") this content represents or null to leave out. Can also contain parameters like "charset=utf-8"

Methods

computeLength()

protected long 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).

Returns
TypeDescription
long
Exceptions
TypeDescription
IOException

getCharset()

protected final Charset getCharset()

Returns the charset specified in the media type or ISO_8859_1 if not specified.

Returns
TypeDescription
Charset

getLength()

public long getLength()

Default implementation calls #computeLength() once and caches it for future invocations, but subclasses may override.

Returns
TypeDescription
long
Exceptions
TypeDescription
IOException

getMediaType()

public final HttpMediaType getMediaType()

Returns the media type to use for the Content-Type header, or null if unspecified.

Returns
TypeDescription
HttpMediaType

getType()

public String getType()

Returns the content type or null for none.

Returns
TypeDescription
String

retrySupported()

public boolean retrySupported()

Default implementation returns true, but subclasses may override.

Returns
TypeDescription
boolean

setMediaType(HttpMediaType mediaType)

public AbstractHttpContent setMediaType(HttpMediaType mediaType)

Sets the media type to use for the Content-Type header, or null 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.

Parameter
NameDescription
mediaTypeHttpMediaType
Returns
TypeDescription
AbstractHttpContent