Top Description Fields Constructors Methods
org.apache.http.util

public final Class EntityUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .InputStreamReader, .Reader, .UnsupportedEncodingException, java.nio.charset.Charset, .UnsupportedCharsetException, org.apache.http.HeaderElement, .HttpEntity, .HttpResponse, .NameValuePair, .ParseException, org.apache.http.entity.ContentType, org.apache.http.protocol.HTTP

Static helpers for dealing with HttpEntitys.
Since
4.0

Field Summary

Modifier and TypeField and Description
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
consume(final HttpEntity
the entity to consume.
entity
)

Ensures that the entity content is fully consumed and the content stream, if exists, is closed.

public static void
consumeQuietly(final HttpEntity
the entity to consume.
entity
)

Ensures that the entity content is fully consumed and the content stream, if exists, is closed.

public static String

Returns:

the character set, or null if not found
getContentCharSet
(final HttpEntity
must not be null
entity
)

Deprecated (4.1.3) use ContentType#getOrDefault(HttpEntity)
Obtains character set of the entity, if known.
public static String

Returns:

the character set, or null if not found
getContentMimeType
(final HttpEntity
must not be null
entity
)

Deprecated (4.1.3) use ContentType#getOrDefault(HttpEntity)
Obtains MIME type of the entity, if known.
public static byte[]

Returns:

byte array containing the entity content. May be null if HttpEntity#getContent() is null.
toByteArray
(final HttpEntity
the entity to read from=
entity
)

Read the contents of an entity and return it as a byte array.

private static String
toString(final HttpEntity entity, final ContentType contentType)

public static String

Returns:

the entity content as a String. May be null if HttpEntity#getContent() is null.
toString
(final HttpEntity
must not be null
entity
,
final Charset
character set to be applied if none found in the entity, or if the entity provided charset is invalid or not available.
defaultCharset
)

Get the entity content as a String, using the provided default character set if none is found in the entity.

public static String

Returns:

the entity content as a String. May be null if HttpEntity#getContent() is null.
toString
(final HttpEntity
must not be null
entity
,
final String
character set to be applied if none found in the entity
defaultCharset
)

Get the entity content as a String, using the provided default character set if none is found in the entity.

public static String

Returns:

String containing the content.
toString
(final HttpEntity
the entity to convert to a string; must not be null
entity
)

Read the contents of an entity and return it as a String.

public static void
updateEntity(final HttpResponse
the response with an entity to update; must not be null.
response
,
final HttpEntity
the entity to set in the response.
entity
)

Updates an entity in a response by first consuming an existing entity, then setting the new one.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_BUFFER_SIZEback to summary
private static final int DEFAULT_BUFFER_SIZE

Constructor Detail

EntityUtilsback to summary
private EntityUtils()

Method Detail

consumeback to summary
public static void consume(final HttpEntity entity) throws IOException

Ensures that the entity content is fully consumed and the content stream, if exists, is closed.

Parameters
entity:HttpEntity

the entity to consume.

Exceptions
IOException:
if an error occurs reading the input stream
Since
4.1
consumeQuietlyback to summary
public static void consumeQuietly(final 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:HttpEntity

the entity to consume.

Since
4.2
getContentCharSetback to summary
public static String getContentCharSet(final HttpEntity entity) throws ParseException

Deprecated

(4.1.3) use ContentType#getOrDefault(HttpEntity)

Obtains character set of the entity, if known.

Parameters
entity:HttpEntity

must not be null

Returns:String

the character set, or null if not found

Annotations
@Deprecated
Exceptions
ParseException:
if header elements cannot be parsed
IllegalArgumentException:
if entity is null
getContentMimeTypeback to summary
public static String getContentMimeType(final HttpEntity entity) throws ParseException

Deprecated

(4.1.3) use ContentType#getOrDefault(HttpEntity)

Obtains MIME type of the entity, if known.

Parameters
entity:HttpEntity

must not be null

Returns:String

the character set, or null if not found

Annotations
@Deprecated
Exceptions
ParseException:
if header elements cannot be parsed
IllegalArgumentException:
if entity is null
Since
4.1
toByteArrayback to summary
public static byte[] toByteArray(final HttpEntity entity) throws IOException

Read the contents of an entity and return it as a byte array.

Parameters
entity:HttpEntity

the entity to read from=

Returns:byte[]

byte array containing the entity content. May be null if HttpEntity#getContent() is null.

Exceptions
IOException:
if an error occurs reading the input stream
IllegalArgumentException:
if entity is null or if content length > Integer.MAX_VALUE
toStringback to summary
private static String toString(final HttpEntity entity, final ContentType contentType) throws IOException
toStringback to summary
public static String toString(final HttpEntity entity, final Charset defaultCharset) throws 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:HttpEntity

must not be null

defaultCharset:Charset

character set to be applied if none found in the entity, or if the entity provided charset is invalid or not available.

Returns:String

the entity content as a String. May be null if HttpEntity#getContent() is null.

Exceptions
IOException:
if an error occurs reading the input stream
ParseException:
if header elements cannot be parsed
IllegalArgumentException:
if entity is null or if content length > Integer.MAX_VALUE
UnsupportedCharsetException:
Thrown when the named entity's charset is not available in this instance of the Java virtual machine and no defaultCharset is provided.
toStringback to summary
public static String toString(final HttpEntity entity, final String defaultCharset) throws 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:HttpEntity

must not be null

defaultCharset:String

character set to be applied if none found in the entity

Returns:String

the entity content as a String. May be null if HttpEntity#getContent() is null.

Exceptions
IOException:
if an error occurs reading the input stream
ParseException:
if header elements cannot be parsed
IllegalArgumentException:
if entity is null or if content length > Integer.MAX_VALUE
UnsupportedCharsetException:
Thrown when the named charset is not available in this instance of the Java virtual machine
toStringback to summary
public static String toString(final HttpEntity entity) throws 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:HttpEntity

the entity to convert to a string; must not be null

Returns:String

String containing the content.

Exceptions
IOException:
if an error occurs reading the input stream
ParseException:
if header elements cannot be parsed
IllegalArgumentException:
if entity is null or if content length > Integer.MAX_VALUE
UnsupportedCharsetException:
Thrown when the named charset is not available in this instance of the Java virtual machine
updateEntityback to summary
public static void updateEntity(final HttpResponse response, final HttpEntity entity) throws IOException

Updates an entity in a response by first consuming an existing entity, then setting the new one.

Parameters
response:HttpResponse

the response with an entity to update; must not be null.

entity:HttpEntity

the entity to set in the response.

Exceptions
IOException:
if an error occurs while reading the input stream on the existing entity.
IllegalArgumentException:
if response is null.
Since
4.3