Class EntityUtils


  • public final class EntityUtils
    extends java.lang.Object
    Support methods for HttpEntity.
    Since:
    4.0
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private EntityUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static long checkContentLength​(EntityDetails entityDetails)  
      static void consume​(HttpEntity entity)
      Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
      static void consumeQuietly​(HttpEntity entity)
      Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
      static java.util.List<NameValuePair> parse​(HttpEntity entity)
      Returns a list of NameValuePairs as parsed from an HttpEntity.
      static java.util.List<NameValuePair> parse​(HttpEntity entity, int maxStreamLength)
      Returns a list of NameValuePairs as parsed from an HttpEntity.
      static byte[] toByteArray​(HttpEntity entity)
      Reads the contents of an entity and return it as a byte array.
      static byte[] toByteArray​(HttpEntity entity, int maxResultLength)
      Reads the contents of an entity and return it as a byte array.
      private static CharArrayBuffer toCharArrayBuffer​(java.io.InputStream inStream, int contentLength, java.nio.charset.Charset charset, int maxResultLength)  
      private static int toContentLength​(int contentLength)
      Gets a usable content length value for the given candidate.
      static java.lang.String toString​(HttpEntity entity)
      Reads the contents of an entity and return it as a String.
      static java.lang.String toString​(HttpEntity entity, int maxResultLength)
      Reads the contents of an entity and return it as a String.
      static java.lang.String toString​(HttpEntity entity, java.lang.String defaultCharset)
      Gets the entity content as a String, using the provided default character set if none is found in the entity.
      static java.lang.String toString​(HttpEntity entity, java.lang.String defaultCharset, int maxResultLength)
      Gets the entity content as a String, using the provided default character set if none is found in the entity.
      static java.lang.String toString​(HttpEntity entity, java.nio.charset.Charset defaultCharset)
      Gets the entity content as a String, using the provided default character set if none is found in the entity.
      static java.lang.String toString​(HttpEntity entity, java.nio.charset.Charset defaultCharset, int maxResultLength)
      Gets the entity content as a String, using the provided default character set if none is found in the entity.
      private static java.lang.String toString​(HttpEntity entity, ContentType contentType, int maxResultLength)  
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_ENTITY_RETURN_MAX_LENGTH

        private static final int DEFAULT_ENTITY_RETURN_MAX_LENGTH
        See Also:
        Constant Field Values
      • DEFAULT_CHARSET

        private static final java.nio.charset.Charset DEFAULT_CHARSET
      • DEFAULT_CHAR_BUFFER_SIZE

        private static final int DEFAULT_CHAR_BUFFER_SIZE
        See Also:
        Constant Field Values
      • DEFAULT_BYTE_BUFFER_SIZE

        private static final int DEFAULT_BYTE_BUFFER_SIZE
        See Also:
        Constant Field Values
      • CONTENT_TYPE_MAP

        private static final java.util.Map<java.lang.String,​ContentType> CONTENT_TYPE_MAP
    • Constructor Detail

      • EntityUtils

        private EntityUtils()
    • Method Detail

      • consumeQuietly

        public static void consumeQuietly​(HttpEntity entity)
        Ensures that the entity content is fully consumed and the content stream, if exists, is closed. The process is done, quietly , without throwing any IOException.
        Parameters:
        entity - the entity to consume.
        Since:
        4.2
      • consume

        public static void consume​(HttpEntity entity)
                            throws java.io.IOException
        Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
        Parameters:
        entity - the entity to consume.
        Throws:
        java.io.IOException - if an error occurs reading the input stream
        Since:
        4.1
      • toContentLength

        private static int toContentLength​(int contentLength)
        Gets a usable content length value for the given candidate.
        Parameters:
        contentLength - an integer.
        Returns:
        The given content length or 4096 if it is < 0.
      • checkContentLength

        static long checkContentLength​(EntityDetails entityDetails)
      • toByteArray

        public static byte[] toByteArray​(HttpEntity entity)
                                  throws java.io.IOException
        Reads the contents of an entity and return it as a byte array.
        Parameters:
        entity - the entity to read from=
        Returns:
        byte array containing the entity content. May be null if HttpEntity.getContent() is null.
        Throws:
        java.io.IOException - if an error occurs reading the input stream
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
      • toByteArray

        public static byte[] toByteArray​(HttpEntity entity,
                                         int maxResultLength)
                                  throws java.io.IOException
        Reads the contents of an entity and return it as a byte array.
        Parameters:
        entity - the entity to read from=
        maxResultLength - The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
        Returns:
        byte array containing the entity content. May be null if HttpEntity.getContent() is null.
        Throws:
        java.io.IOException - if an error occurs reading the input stream
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
      • toCharArrayBuffer

        private static CharArrayBuffer toCharArrayBuffer​(java.io.InputStream inStream,
                                                         int contentLength,
                                                         java.nio.charset.Charset charset,
                                                         int maxResultLength)
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • toString

        private static java.lang.String toString​(HttpEntity entity,
                                                 ContentType contentType,
                                                 int maxResultLength)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • toString

        public static java.lang.String toString​(HttpEntity entity,
                                                java.nio.charset.Charset defaultCharset)
                                         throws java.io.IOException,
                                                ParseException
        Gets the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
        Parameters:
        entity - must not be null
        defaultCharset - character set to be applied if none found in the entity, or if the entity provided charset is invalid or not available.
        Returns:
        the entity content as a String. May be null if HttpEntity.getContent() is null.
        Throws:
        ParseException - if header elements cannot be parsed
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
        java.io.IOException - if an error occurs reading the input stream
        java.nio.charset.UnsupportedCharsetException - Thrown when the named entity's charset is not available in this instance of the Java virtual machine and no defaultCharset is provided.
      • toString

        public static java.lang.String toString​(HttpEntity entity,
                                                java.nio.charset.Charset defaultCharset,
                                                int maxResultLength)
                                         throws java.io.IOException,
                                                ParseException
        Gets the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
        Parameters:
        entity - must not be null
        defaultCharset - character set to be applied if none found in the entity, or if the entity provided charset is invalid or not available.
        maxResultLength - The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
        Returns:
        the entity content as a String. May be null if HttpEntity.getContent() is null.
        Throws:
        ParseException - if header elements cannot be parsed
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
        java.io.IOException - if an error occurs reading the input stream
        java.nio.charset.UnsupportedCharsetException - Thrown when the named entity's charset is not available in this instance of the Java virtual machine and no defaultCharset is provided.
      • toString

        public static java.lang.String toString​(HttpEntity entity,
                                                java.lang.String defaultCharset)
                                         throws java.io.IOException,
                                                ParseException
        Gets the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
        Parameters:
        entity - must not be null
        defaultCharset - character set to be applied if none found in the entity
        Returns:
        the entity content as a String. May be null if HttpEntity.getContent() is null.
        Throws:
        ParseException - if header elements cannot be parsed
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
        java.io.IOException - if an error occurs reading the input stream
        java.nio.charset.UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
      • toString

        public static java.lang.String toString​(HttpEntity entity,
                                                java.lang.String defaultCharset,
                                                int maxResultLength)
                                         throws java.io.IOException,
                                                ParseException
        Gets the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
        Parameters:
        entity - must not be null
        defaultCharset - character set to be applied if none found in the entity
        maxResultLength - The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
        Returns:
        the entity content as a String. May be null if HttpEntity.getContent() is null.
        Throws:
        ParseException - if header elements cannot be parsed
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
        java.io.IOException - if an error occurs reading the input stream
        java.nio.charset.UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
      • toString

        public static java.lang.String toString​(HttpEntity entity)
                                         throws java.io.IOException,
                                                ParseException
        Reads the contents of an entity and return it as a String. The content is converted using the character set from the entity (if any), failing that, "ISO-8859-1" is used.
        Parameters:
        entity - the entity to convert to a string; must not be null
        Returns:
        String containing the content.
        Throws:
        ParseException - if header elements cannot be parsed
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
        java.io.IOException - if an error occurs reading the input stream
        java.nio.charset.UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
      • toString

        public static java.lang.String toString​(HttpEntity entity,
                                                int maxResultLength)
                                         throws java.io.IOException,
                                                ParseException
        Reads the contents of an entity and return it as a String. The content is converted using the character set from the entity (if any), failing that, "ISO-8859-1" is used.
        Parameters:
        entity - the entity to convert to a string; must not be null
        maxResultLength - The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
        Returns:
        String containing the content.
        Throws:
        ParseException - if header elements cannot be parsed
        java.lang.IllegalArgumentException - if entity is null or if content length > Integer.MAX_VALUE
        java.io.IOException - if an error occurs reading the input stream
        java.nio.charset.UnsupportedCharsetException - Thrown when the named charset is not available in this instance of the Java virtual machine
      • parse

        public static java.util.List<NameValuePair> parse​(HttpEntity entity)
                                                   throws java.io.IOException
        Returns a list of NameValuePairs as parsed from an HttpEntity. The encoding is taken from the entity's Content-Encoding header.

        This is typically used while parsing an HTTP POST.

        Parameters:
        entity - The entity to parse
        Returns:
        a list of NameValuePair as built from the URI's query portion.
        Throws:
        java.io.IOException - If there was an exception getting the entity's data.
      • parse

        public static java.util.List<NameValuePair> parse​(HttpEntity entity,
                                                          int maxStreamLength)
                                                   throws java.io.IOException
        Returns a list of NameValuePairs as parsed from an HttpEntity. The encoding is taken from the entity's Content-Encoding header.

        This is typically used while parsing an HTTP POST.

        Parameters:
        entity - The entity to parse
        maxStreamLength - The maximum size of the stream to read; use it to guard against unreasonable or malicious processing.
        Returns:
        a list of NameValuePair as built from the URI's query portion.
        Throws:
        java.io.IOException - If there was an exception getting the entity's data.