org.apache.hc.client5.http.entity

## Class EntityBuilder

- java.lang.Object
- - org.apache.hc.client5.http.entity.EntityBuilder

    public class EntityBuilder
    extends Object

Builder for `HttpEntity` instances.

Several setter methods of this builder are mutually exclusive. In case of multiple invocations of the following methods only the last one will have effect:

- `setText(String)`
- `setBinary(byte[])`
- `setStream(java.io.InputStream)`
- `setSerializable(java.io.Serializable)`
- `setParameters(java.util.List)`
- `setParameters(NameValuePair...)`
- `setFile(java.io.File)`

Since:
  4.3

### Method Summary

All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
`HttpEntity` `build()` Builds a new instance of `HttpEntity` based on the current state.
`EntityBuilder` `chunked()` Sets entities to be chunked.
`EntityBuilder` `compressed(ContentCoding coding)` Requests that the entity produced by this builder be compressed with the supplied content-coding.
`static EntityBuilder` `create()`
`byte[]` `getBinary()` Gets entity content as a byte array if set using `setBinary(byte[])`.
`ContentCoding` `getCompressWith()` Returns the content-coding that `build()` will apply to the outgoing `HttpEntity`, or `null` when no compression has been requested.
`String` `getContentEncoding()` Gets the content encoding of the entity, may be null.
`ContentType` `getContentType()` Gets the `ContentType` of the entity, may be null.
`File` `getFile()` Gets the entity content as a `File` if set using `setFile(java.io.File)`.
`List<NameValuePair>` `getParameters()` Gets entity content as a parameter list if set using `setParameters(java.util.List)` or `setParameters(NameValuePair...)`.
`Serializable` `getSerializable()` Gets entity content as a `Serializable` if set using `setSerializable(java.io.Serializable)` method.
`InputStream` `getStream()` Gets entity content as an `InputStream` if set using `setStream(java.io.InputStream)` method.
`String` `getText()` Gets the entity content as a string if set using `setText(String)`.
`EntityBuilder` `gzipCompressed()` Deprecated. since 5.6 – replace with `compressed(ContentCoding.GZIP)`.
`boolean` `isChunked()` Tests if the entity is to be chunk coded (`true`), or not (`false`).
`boolean` `isGzipCompressed()` Deprecated. since 5.6 – use `getCompressWith()` and check for `ContentCoding.GZIP` instead.
`EntityBuilder` `setBinary(byte[] binary)` Sets entity content as a byte array.
`EntityBuilder` `setContentEncoding(String contentEncoding)` Sets the content encoding of the entity.
`EntityBuilder` `setContentType(ContentType contentType)` Sets the `ContentType` of the entity.
`EntityBuilder` `setFile(File file)` Sets entity content as a `File`.
`EntityBuilder` `setParameters(List<NameValuePair> parameters)` Sets entity content as a parameter list.
`EntityBuilder` `setParameters(NameValuePair... parameters)` Sets entity content as a parameter list.
`EntityBuilder` `setSerializable(Serializable serializable)` Sets entity content as a `Serializable`.
`EntityBuilder` `setStream(InputStream stream)` Sets entity content as an `InputStream`.
`EntityBuilder` `setText(String text)` Sets entity content as a string.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### create

    public static EntityBuilder create()

#### getText

    public String getText()

Gets the entity content as a string if set using `setText(String)`.

Returns:
  the entity content as a string, may be null.

#### setText

    public EntityBuilder setText(String text)

Sets entity content as a string. This method is mutually exclusive with `setBinary(byte[])`, `setStream(java.io.InputStream)`, `setSerializable(java.io.Serializable)`, `setParameters(java.util.List)`, `setParameters(NameValuePair...)` `setFile(java.io.File)` methods.

Parameters:
  `text` - entity content as a string.
Returns:
  this instance.

#### getBinary

    public byte[] getBinary()

Gets entity content as a byte array if set using `setBinary(byte[])`.

Returns:
  entity content as a byte array.

#### setBinary

    public EntityBuilder setBinary(byte[] binary)

Sets entity content as a byte array. This method is mutually exclusive with `setText(String)`, `setStream(java.io.InputStream)`, `setSerializable(java.io.Serializable)`, `setParameters(java.util.List)`, `setParameters(NameValuePair...)` `setFile(java.io.File)`.

Parameters:
  `binary` - The new entity content as a byte array.
Returns:
  this instance.

#### getStream

    public InputStream getStream()

Gets entity content as an `InputStream` if set using `setStream(java.io.InputStream)` method.

Returns:
  entity content as an `InputStream`

#### setStream

    public EntityBuilder setStream(InputStream stream)

Sets entity content as an `InputStream`. This method is mutually exclusive with `setText(String)`, `setBinary(byte[])`, `setSerializable(java.io.Serializable)`, `setParameters(java.util.List)`, `setParameters(NameValuePair...)` `setFile(java.io.File)`.

Parameters:
  `stream` - The new entity content as an InputStream.
Returns:
  this instance.

#### getParameters

    public List<NameValuePair> getParameters()

Gets entity content as a parameter list if set using `setParameters(java.util.List)` or `setParameters(NameValuePair...)`.

Returns:
  entity content as a parameter list.

#### setParameters

    public EntityBuilder setParameters(List<NameValuePair> parameters)

Sets entity content as a parameter list. This method is mutually exclusive with `setText(String)`, `setBinary(byte[])`, `setStream(java.io.InputStream)`, `setSerializable(java.io.Serializable)`, `setFile(java.io.File)`.

Parameters:
  `parameters` - entity content as a parameter list.
Returns:
  this instance.

#### setParameters

    public EntityBuilder setParameters(NameValuePair... parameters)

Sets entity content as a parameter list. This method is mutually exclusive with `setText(String)`, `setBinary(byte[])`, `setStream(java.io.InputStream)`, `setSerializable(java.io.Serializable)`, `setFile(java.io.File)`.

Parameters:
  `parameters` - entity content as a parameter list.
Returns:
  this

#### getSerializable

    public Serializable getSerializable()

Gets entity content as a `Serializable` if set using `setSerializable(java.io.Serializable)` method.

Returns:
  entity content as a `Serializable`.

#### setSerializable

    public EntityBuilder setSerializable(Serializable serializable)

Sets entity content as a `Serializable`. This method is mutually exclusive with `setText(String)`, `setBinary(byte[])`, `setStream(java.io.InputStream)`, `setParameters(java.util.List)`, `setParameters(NameValuePair...)` `setFile(java.io.File)`.

Parameters:
  `serializable` - entity content as a `Serializable`.
Returns:
  this instance.

#### getFile

    public File getFile()

Gets the entity content as a `File` if set using `setFile(java.io.File)`.

Returns:
  Gets the entity content as a `File`.

#### setFile

    public EntityBuilder setFile(File file)

Sets entity content as a `File`. This method is mutually exclusive with `setText(String)`, `setBinary(byte[])`, `setStream(java.io.InputStream)`, `setParameters(java.util.List)`, `setParameters(NameValuePair...)` `setSerializable(java.io.Serializable)`.

Parameters:
  `file` - entity content as a `File`.
Returns:
  this instance.

#### getContentType

    public ContentType getContentType()

Gets the `ContentType` of the entity, may be null.

Returns:
  the `ContentType` of the entity, may be null.

#### setContentType

    public EntityBuilder setContentType(ContentType contentType)

Sets the `ContentType` of the entity.

Parameters:
  `contentType` - the `ContentType` of the entity, may be null.
Returns:
  this instance.

#### getContentEncoding

    public String getContentEncoding()

Gets the content encoding of the entity, may be null.

Returns:
  the content encoding of the entity, may be null.

#### setContentEncoding

    public EntityBuilder setContentEncoding(String contentEncoding)

Sets the content encoding of the entity.

Parameters:
  `contentEncoding` - the content encoding of the entity, may be null.
Returns:
  this instance.

#### isChunked

    public boolean isChunked()

Tests if the entity is to be chunk coded (`true`), or not (`false`).

Returns:
  `true` if entity is to be chunk coded, `false` otherwise.

#### chunked

    public EntityBuilder chunked()

Sets entities to be chunked.

Returns:
  this instance.

#### isGzipCompressed

    @Deprecated
    public boolean isGzipCompressed()

Deprecated. since 5.6 – use `getCompressWith()` and check for `ContentCoding.GZIP` instead.
Tests if entities are to be GZIP compressed (`true`), or not (`false`).

Returns:
  `true` if entity is to be GZIP compressed, `false` otherwise.

#### gzipCompressed

    @Deprecated
    public EntityBuilder gzipCompressed()

Deprecated. since 5.6 – replace with `compressed(ContentCoding.GZIP)`.
Sets entities to be GZIP compressed.

Returns:
  this instance.

#### compressed

    public EntityBuilder compressed(ContentCoding coding)

Requests that the entity produced by this builder be compressed with the supplied content-coding.

Parameters:
  `coding` - the content-coding to use (never `null`)
Returns:
  this builder for method chaining
Since:
  5.6

#### getCompressWith

    public ContentCoding getCompressWith()

Returns the content-coding that `build()` will apply to the outgoing `HttpEntity`, or `null` when no compression has been requested.

Returns:
  the chosen `ContentCoding` — typically `ContentCoding.GZIP`, `ContentCoding.DEFLATE`, etc. — or `null` if the request body will be sent uncompressed.
Since:
  5.6

#### build

    public HttpEntity build()

Builds a new instance of `HttpEntity` based on the current state.

Returns:
  a new instance.