Top Description Fields Constructors Methods
org.apache.hc.core5.http.io.entity

public final Class EntityUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .InputStreamReader, .Reader, .UnsupportedEncodingException, java.nio.charset.Charset, .StandardCharsets, .UnsupportedCharsetException, java.util.Collections, .HashMap, .List, .Map, org.apache.hc.core5.http.ContentType, .EntityDetails, .HttpEntity, .NameValuePair, .ParseException, org.apache.hc.core5.io.Closer, org.apache.hc.core5.net.WWWFormCodec, org.apache.hc.core5.util.Args, .ByteArrayBuffer, .CharArrayBuffer

Support methods for HttpEntity.
Since
4.0

Field Summary

Modifier and TypeField and Description
private static final Map<String, ContentType>
private static final int
private static final int
private static final Charset
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static long
checkContentLength(final EntityDetails entityDetails)

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 List<NameValuePair>

Returns:

a list of NameValuePair as built from the URI's query portion.
parse
(final HttpEntity
The entity to parse
entity
)

Returns a list of NameValuePairs as parsed from an HttpEntity.

public static List<NameValuePair>

Returns:

a list of NameValuePair as built from the URI's query portion.
parse
(final HttpEntity
The entity to parse
entity
,
final int
The maximum size of the stream to read; use it to guard against unreasonable or malicious processing.
maxStreamLength
)

Returns a list of NameValuePairs as parsed from an HttpEntity.

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
)

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

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
,
final int
The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
maxResultLength
)

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

private static CharArrayBuffer
toCharArrayBuffer(final InputStream inStream, final int contentLength, final Charset charset, final int maxResultLength)

private static int

Returns:

The given content length or 4096 if it is < 0.
toContentLength
(final int
an integer.
contentLength
)

Gets a usable content length value for the given candidate.

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

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
)

Gets 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 Charset
character set to be applied if none found in the entity, or if the entity provided charset is invalid or not available.
defaultCharset
,
final int
The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
maxResultLength
)

Gets 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
)

Gets 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
,
final int
The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
maxResultLength
)

Gets 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
)

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

public static String

Returns:

String containing the content.
toString
(final HttpEntity
the entity to convert to a string; must not be null
entity
,
final int
The maximum size of the String to return; use it to guard against unreasonable or malicious processing.
maxResultLength
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CONTENT_TYPE_MAPback to summary
private static final Map<String, ContentType> CONTENT_TYPE_MAP
DEFAULT_BYTE_BUFFER_SIZEback to summary
private static final int DEFAULT_BYTE_BUFFER_SIZE
DEFAULT_CHAR_BUFFER_SIZEback to summary
private static final int DEFAULT_CHAR_BUFFER_SIZE
DEFAULT_CHARSETback to summary
private static final Charset DEFAULT_CHARSET
DEFAULT_ENTITY_RETURN_MAX_LENGTHback to summary
private static final int DEFAULT_ENTITY_RETURN_MAX_LENGTH

Constructor Detail

EntityUtilsback to summary
private EntityUtils()

Method Detail

checkContentLengthback to summary
pack-priv static long checkContentLength(final EntityDetails entityDetails)
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
parseback to summary
public static List<NameValuePair> parse(final HttpEntity entity) throws 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:HttpEntity

The entity to parse

Returns:List<NameValuePair>

a list of NameValuePair as built from the URI's query portion.

Exceptions
IOException:
If there was an exception getting the entity's data.
parseback to summary
public static List<NameValuePair> parse(final HttpEntity entity, final int maxStreamLength) throws 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:HttpEntity

The entity to parse

maxStreamLength:int

The maximum size of the stream to read; use it to guard against unreasonable or malicious processing.

Returns:List<NameValuePair>

a list of NameValuePair as built from the URI's query portion.

Exceptions
IOException:
If there was an exception getting the entity's data.
toByteArrayback to summary
public static byte[] toByteArray(final HttpEntity entity) throws IOException

Reads 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
toByteArrayback to summary
public static byte[] toByteArray(final HttpEntity entity, final int maxResultLength) throws IOException

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

Parameters
entity:HttpEntity

the entity to read from=

maxResultLength:int

The maximum size of the String to return; use it to guard against unreasonable or malicious processing.

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
toCharArrayBufferback to summary
private static CharArrayBuffer toCharArrayBuffer(final InputStream inStream, final int contentLength, final Charset charset, final int maxResultLength) throws IOException
toContentLengthback to summary
private static int toContentLength(final int contentLength)

Gets a usable content length value for the given candidate.

Parameters
contentLength:int

an integer.

Returns:int

The given content length or 4096 if it is < 0.

toStringback to summary
private static String toString(final HttpEntity entity, final ContentType contentType, final int maxResultLength) throws IOException
toStringback to summary
public static String toString(final HttpEntity entity, final Charset defaultCharset) throws 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: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 Charset defaultCharset, final int maxResultLength) throws 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: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.

maxResultLength:int

The maximum size of the String to return; use it to guard against unreasonable or malicious processing.

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

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: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, final String defaultCharset, final int maxResultLength) throws 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:HttpEntity

must not be null

defaultCharset:String

character set to be applied if none found in the entity

maxResultLength:int

The maximum size of the String to return; use it to guard against unreasonable or malicious processing.

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

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: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
toStringback to summary
public static String toString(final HttpEntity entity, final int maxResultLength) throws 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:HttpEntity

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

maxResultLength:int

The maximum size of the String to return; use it to guard against unreasonable or malicious processing.

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