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 Object implements HttpContent
Abstract implementation of an HTTP content with typical options.

Implementation is not thread-safe.

Since:
1.5
  • Field Details

    • mediaType

      private HttpMediaType mediaType
      Media type used for the Content-Type header or null for none.
    • computedLength

      private long computedLength
      Cached value for the computed length from computeLength().
  • Constructor Details

    • AbstractHttpContent

      protected AbstractHttpContent(String mediaType)
      Parameters:
      mediaType - Media type string (for example "type/subtype") this content represents or null to 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 or null to leave out
      Since:
      1.10
  • Method Details

    • getLength

      public long getLength() throws IOException
      Default implementation calls computeLength() once and caches it for future invocations, but subclasses may override.
      Specified by:
      getLength in interface HttpContent
      Throws:
      IOException
    • getMediaType

      public final HttpMediaType getMediaType()
      Returns the media type to use for the Content-Type header, or null if unspecified.
      Since:
      1.10
    • setMediaType

      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.

      Since:
      1.10
    • getCharset

      protected final Charset getCharset()
      Returns the charset specified in the media type or Charsets#UTF_8 if not specified.
      Since:
      1.10
    • getType

      public String getType()
      Description copied from interface: HttpContent
      Returns the content type or null for none.
      Specified by:
      getType in interface HttpContent
    • computeLength

      protected long computeLength() throws IOException
      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 returns true, but subclasses may override.
      Specified by:
      retrySupported in interface HttpContent
    • computeLength

      public static long computeLength(HttpContent content) throws IOException
      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.
      Parameters:
      content - HTTP content
      Returns:
      computed content length or -1 if retry is not supported
      Throws:
      IOException
      Since:
      1.14