org.apache.http.util
Class EntityUtils

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

public final class EntityUtils
extends java.lang.Object

Static helpers for dealing with HttpEntitys.

Since:
4.0

Method Summary
static void consume(HttpEntity entity)
          Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
static java.lang.String getContentCharSet(HttpEntity entity)
          Obtains character set of the entity, if known.
static java.lang.String getContentMimeType(HttpEntity entity)
          Obtains mime type of the entity, if known.
static byte[] toByteArray(HttpEntity entity)
          Read the contents of an entity and return it as a byte array.
static java.lang.String toString(HttpEntity entity)
          Read the contents of an entity and return it as a String.
static java.lang.String toString(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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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 -
Throws:
java.io.IOException - if an error occurs reading the input stream
Since:
4.1

toByteArray

public static byte[] toByteArray(HttpEntity entity)
                          throws java.io.IOException
Read the contents of an entity and return it as a byte array.

Parameters:
entity -
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

public static java.lang.String getContentCharSet(HttpEntity entity)
                                          throws ParseException
Obtains character set of the entity, if known.

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

getContentMimeType

public static java.lang.String getContentMimeType(HttpEntity entity)
                                           throws ParseException
Obtains mime type of the entity, if known.

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

toString

public static java.lang.String toString(HttpEntity entity,
                                        java.lang.String defaultCharset)
                                 throws java.io.IOException,
                                        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:
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

toString

public static java.lang.String toString(HttpEntity entity)
                                 throws java.io.IOException,
                                        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 -
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


Copyright © 2005-2011 The Apache Software Foundation. All Rights Reserved.