org.apache.http.util
Class EntityUtilsHC4

java.lang.Object
  extended by org.apache.http.util.EntityUtilsHC4

public final class EntityUtilsHC4
extends java.lang.Object

Static helpers for dealing with HttpEntitys.

Since:
4.0

Method Summary
static void consume(org.apache.http.HttpEntity entity)
          Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
static void consumeQuietly(org.apache.http.HttpEntity entity)
          Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
static java.lang.String getContentCharSet(org.apache.http.HttpEntity entity)
          Deprecated. (4.1.3) use ContentType.getOrDefault(HttpEntity)
static java.lang.String getContentMimeType(org.apache.http.HttpEntity entity)
          Deprecated. (4.1.3) use ContentType.getOrDefault(HttpEntity)
static byte[] toByteArray(org.apache.http.HttpEntity entity)
          Read the contents of an entity and return it as a byte array.
static java.lang.String toString(org.apache.http.HttpEntity entity)
          Read the contents of an entity and return it as a String.
static java.lang.String toString(org.apache.http.HttpEntity entity, java.nio.charset.Charset defaultCharset)
          Get the entity content as a String, using the provided default character set if none is found in the entity.
static java.lang.String toString(org.apache.http.HttpEntity entity, java.lang.String defaultCharset)
          Get the entity content as a String, using the provided default character set if none is found in the entity.
static void updateEntity(org.apache.http.HttpResponse response, org.apache.http.HttpEntity entity)
          Updates an entity in a response by first consuming an existing entity, then setting the new one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

consumeQuietly

public static void consumeQuietly(org.apache.http.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(org.apache.http.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

updateEntity

public static void updateEntity(org.apache.http.HttpResponse response,
                                org.apache.http.HttpEntity entity)
                         throws java.io.IOException
Updates an entity in a response by first consuming an existing entity, then setting the new one.

Parameters:
response - the response with an entity to update; must not be null.
entity - the entity to set in the response.
Throws:
java.io.IOException - if an error occurs while reading the input stream on the existing entity.
java.lang.IllegalArgumentException - if response is null.
Since:
4.3

toByteArray

public static byte[] toByteArray(org.apache.http.HttpEntity entity)
                          throws java.io.IOException
Read 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

getContentCharSet

@Deprecated
public static java.lang.String getContentCharSet(org.apache.http.HttpEntity entity)
                                          throws org.apache.http.ParseException
Deprecated. (4.1.3) use ContentType.getOrDefault(HttpEntity)

Obtains character set of the entity, if known.

Parameters:
entity - must not be null
Returns:
the character set, or null if not found
Throws:
org.apache.http.ParseException - if header elements cannot be parsed
java.lang.IllegalArgumentException - if entity is null

getContentMimeType

@Deprecated
public static java.lang.String getContentMimeType(org.apache.http.HttpEntity entity)
                                           throws org.apache.http.ParseException
Deprecated. (4.1.3) use ContentType.getOrDefault(HttpEntity)

Obtains MIME type of the entity, if known.

Parameters:
entity - must not be null
Returns:
the character set, or null if not found
Throws:
org.apache.http.ParseException - if header elements cannot be parsed
java.lang.IllegalArgumentException - if entity is null
Since:
4.1

toString

public static java.lang.String toString(org.apache.http.HttpEntity entity,
                                        java.nio.charset.Charset defaultCharset)
                                 throws java.io.IOException,
                                        org.apache.http.ParseException
Get 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:
org.apache.http.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(org.apache.http.HttpEntity entity,
                                        java.lang.String defaultCharset)
                                 throws java.io.IOException,
                                        org.apache.http.ParseException
Get 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:
org.apache.http.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(org.apache.http.HttpEntity entity)
                                 throws java.io.IOException,
                                        org.apache.http.ParseException
Read 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:
org.apache.http.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