Class HttpContent

    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static HttpContent bytes​(byte[] content)
      Create a new content from the given bytes.
      static HttpContent bytes​(byte[] content, int offset, int length)
      Create a new content from the given bytes.
      abstract boolean canBeStreamedMultipleTimes()
      Get if the content can be streamed multiple times.
      The return value is allowed to change between multiple calls depending on the implementation.
      protected void clearCache()
      Clear the cached byte array if present.
      This will force the content to be recomputed on the next access.
      This should only be used if canBeStreamedMultipleTimes() returns true.
      protected abstract java.io.InputStream compute()
      Compute the content and return it as an input stream.
      This method can be called multiple times if canBeStreamedMultipleTimes() returns true.
      static HttpContent file​(java.io.File file)
      Create a new content from the given file.
      static HttpContent form​(java.lang.String key, java.lang.String value)
      Create a new content from the given form data.
      static HttpContent form​(java.util.Map<java.lang.String,​java.lang.String> form)
      Create a new content from the given form data.
      byte[] getAsBytes()  
      java.io.InputStream getAsStream()
      Get the content as an input stream.
      You are responsible for closing the input stream after use.
      java.lang.String getAsString()  
      java.lang.String getAsString​(java.nio.charset.Charset charset)
      Get the content as a string with the given charset.
      int getBufferSize()  
      int getContentLength()
      Deprecated.
      ContentType getContentType()
      Deprecated.
      abstract int getLength()
      Get the content length in bytes.
      If the content length is unknown, return -1.
      Streaming the content may require valid content length depending on the implementation.
      ContentType getType()  
      static InputStreamContent inputStream​(ContentType contentType, java.io.InputStream inputStream, int contentLength)
      Create a new input stream content from the given input stream.
      protected java.io.InputStream modify​(java.io.InputStream inputStream)
      Modify the given input stream before returning it to the user.
      This can be used to wrap the input stream with another stream (e.g.
      static MultiPartFormContent multiPartForm()
      Create a new multipart form content.
      HttpContent setBufferSize​(int bufferSize)
      Set the recommended buffer size for streaming the content.
      This is only a recommendation and may be ignored by the implementation.
      static HttpContent string​(java.lang.String content)
      Create a new content from the given string.
      static HttpContent string​(java.lang.String content, java.nio.charset.Charset charset)
      Create a new content from the given string.
      void transferTo​(java.io.OutputStream outputStream)
      Transfer the content to the given output stream.
      The content stream will be closed after the transfer is complete.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HttpContent

        public HttpContent​(ContentType contentType)
    • Method Detail

      • bytes

        public static HttpContent bytes​(byte[] content)
        Create a new content from the given bytes.
        Parameters:
        content - The bytes to send
        Returns:
        The created content
      • bytes

        public static HttpContent bytes​(byte[] content,
                                        int offset,
                                        int length)
        Create a new content from the given bytes.
        Parameters:
        content - The bytes to send
        offset - The offset to start reading from
        length - The length of the bytes to read
        Returns:
        The created content
      • string

        public static HttpContent string​(java.lang.String content)
        Create a new content from the given string.
        Parameters:
        content - The string to send
        Returns:
        The created content
      • string

        public static HttpContent string​(java.lang.String content,
                                         java.nio.charset.Charset charset)
        Create a new content from the given string.
        Parameters:
        content - The string to send
        charset - The charset to use
        Returns:
        The created content
      • file

        public static HttpContent file​(java.io.File file)
        Create a new content from the given file.
        Parameters:
        file - The file to send
        Returns:
        The created content
      • form

        public static HttpContent form​(java.lang.String key,
                                       java.lang.String value)
        Create a new content from the given form data.
        Parameters:
        key - The key
        value - The value
        Returns:
        The created content
      • form

        public static HttpContent form​(java.util.Map<java.lang.String,​java.lang.String> form)
        Create a new content from the given form data.
        Parameters:
        form - The form data
        Returns:
        The created content
      • multiPartForm

        public static MultiPartFormContent multiPartForm()
        Create a new multipart form content.
        Returns:
        The created content
      • inputStream

        public static InputStreamContent inputStream​(ContentType contentType,
                                                     java.io.InputStream inputStream,
                                                     int contentLength)
        Create a new input stream content from the given input stream.
        Parameters:
        contentType - The content type
        inputStream - The input stream
        contentLength - The content length
        Returns:
        The created content
      • getContentType

        @Deprecated
        @ScheduledForRemoval
        public final ContentType getContentType()
        Deprecated.
        Deprecated, use getType() instead.
      • getType

        public final ContentType getType()
        Returns:
        The type of this content
      • getBufferSize

        public final int getBufferSize()
        Returns:
        The recommended buffer size for streaming the content
      • setBufferSize

        public final HttpContent setBufferSize​(int bufferSize)
        Set the recommended buffer size for streaming the content.
        This is only a recommendation and may be ignored by the implementation.
        Parameters:
        bufferSize - The buffer size
        Returns:
        This instance for chaining
      • transferTo

        public final void transferTo​(@WillNotClose
                                     java.io.OutputStream outputStream)
                              throws java.io.IOException
        Transfer the content to the given output stream.
        The content stream will be closed after the transfer is complete.
        Parameters:
        outputStream - The output stream to transfer to
        Throws:
        java.io.IOException - If an I/O error occurs
      • getAsStream

        public final java.io.InputStream getAsStream()
                                              throws java.io.IOException
        Get the content as an input stream.
        You are responsible for closing the input stream after use.
        Returns:
        The content as an input stream
        Throws:
        java.io.IOException - If an I/O error occurs
      • getAsBytes

        @Nonnull
        public final byte[] getAsBytes()
                                throws java.io.IOException
        Returns:
        The content as bytes
        Throws:
        java.io.IOException - If an I/O error occurs
      • getAsString

        @Nonnull
        public final java.lang.String getAsString()
                                           throws java.io.IOException
        Returns:
        The content as a UTF-8 string
        Throws:
        java.io.IOException - If an I/O error occurs
      • getAsString

        @Nonnull
        public final java.lang.String getAsString​(java.nio.charset.Charset charset)
                                           throws java.io.IOException
        Get the content as a string with the given charset.
        Parameters:
        charset - The charset to use
        Returns:
        The content as a string
        Throws:
        java.io.IOException - If an I/O error occurs
      • clearCache

        protected final void clearCache()
        Clear the cached byte array if present.
        This will force the content to be recomputed on the next access.
        This should only be used if canBeStreamedMultipleTimes() returns true.
      • getContentLength

        @Deprecated
        @ScheduledForRemoval
        public final int getContentLength()
        Deprecated.
        Deprecated, use getLength() instead.
      • canBeStreamedMultipleTimes

        public abstract boolean canBeStreamedMultipleTimes()
        Get if the content can be streamed multiple times.
        The return value is allowed to change between multiple calls depending on the implementation.
        Returns:
        If the content can be streamed multiple times
      • getLength

        public abstract int getLength()
        Get the content length in bytes.
        If the content length is unknown, return -1.
        Streaming the content may require valid content length depending on the implementation.
        Returns:
        The content length
      • compute

        @Nonnull
        protected abstract java.io.InputStream compute()
                                                throws java.io.IOException
        Compute the content and return it as an input stream.
        This method can be called multiple times if canBeStreamedMultipleTimes() returns true.
        Returns:
        The content
        Throws:
        java.io.IOException - If an I/O error occurs
      • modify

        protected java.io.InputStream modify​(java.io.InputStream inputStream)
                                      throws java.io.IOException
        Modify the given input stream before returning it to the user.
        This can be used to wrap the input stream with another stream (e.g. for decompression).
        The default implementation just returns the given input stream.
        Parameters:
        inputStream - The input stream to modify
        Returns:
        The modified input stream
        Throws:
        java.io.IOException - If an I/O error occurs