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

public Class StringEntity

extends AbstractHttpEntity
Class Inheritance
Annotations
@Contract
threading:IMMUTABLE
Imports
java.io.ByteArrayInputStream, .IOException, .InputStream, .OutputStream, java.nio.charset.Charset, .StandardCharsets, org.apache.hc.core5.annotation.Contract, .ThreadingBehavior, org.apache.hc.core5.http.ContentType, org.apache.hc.core5.util.Args

A self contained, repeatable entity that obtains its content from a String.
Since
4.0

Field Summary

Modifier and TypeField and Description
private final byte[]
Inherited from org.apache.hc.core5.http.io.entity.AbstractHttpEntity:
OUTPUT_BUFFER_SIZE

Constructor Summary

AccessConstructor and Description
public
StringEntity(final String
content to be used. Not null.
string
,
final ContentType
content type to be used. May be null, in which case the default MIME type ContentType#TEXT_PLAIN is assumed.
contentType
,
final String contentEncoding, final boolean chunked)

Creates a StringEntity with the specified content and content type.

public
StringEntity(final String string, final ContentType contentType, final boolean chunked)

public
StringEntity(final String string, final ContentType contentType)

public
StringEntity(final String
content to be used. Not null.
string
,
final Charset
character set to be used. May be null, in which case the default is StandardCharsets#ISO_8859_1 is assumed
charset
)

Creates a StringEntity with the specified content and charset.

public
StringEntity(final String string, final Charset charset, final boolean chunked)

public
StringEntity(final String
content to be used. Not null.
string
)

Creates a StringEntity with the specified content.

Method Summary

Modifier and TypeMethod and Description
public final void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
public final InputStream
getContent()

Implements org.apache.hc.core5.http.HttpEntity.getContent.

Returns a content stream of the entity.
public final long
getContentLength()

Implements org.apache.hc.core5.http.EntityDetails.getContentLength.

Returns length of the entity, if known.
public final boolean
isRepeatable()

Overrides org.apache.hc.core5.http.io.entity.AbstractHttpEntity.isRepeatable.

Implements org.apache.hc.core5.http.HttpEntity.isRepeatable.

Tells if the entity is capable of producing its data more than once.
public final boolean
isStreaming()

Implements org.apache.hc.core5.http.HttpEntity.isStreaming.

Tells whether this entity depends on an underlying stream.
public final void
writeTo(final OutputStream
the output stream to write entity content to
outStream
)

Overrides org.apache.hc.core5.http.io.entity.AbstractHttpEntity.writeTo.

Implements org.apache.hc.core5.http.HttpEntity.writeTo.

Writes the entity content out to the output stream.
Inherited from org.apache.hc.core5.http.io.entity.AbstractHttpEntity:
getContentEncodinggetContentTypegetTrailerNamesgetTrailersisChunkedtoStringwriteTo

Field Detail

contentback to summary
private final byte[] content

Constructor Detail

StringEntityback to summary
public StringEntity(final String string, final ContentType contentType, final String contentEncoding, final boolean chunked)

Creates a StringEntity with the specified content and content type.

Parameters
string:String

content to be used. Not null.

contentType:ContentType

content type to be used. May be null, in which case the default MIME type ContentType#TEXT_PLAIN is assumed.

Since
5.0
StringEntityback to summary
public StringEntity(final String string, final ContentType contentType, final boolean chunked)
StringEntityback to summary
public StringEntity(final String string, final ContentType contentType)
StringEntityback to summary
public StringEntity(final String string, final Charset charset)

Creates a StringEntity with the specified content and charset. The MIME type defaults to "text/plain".

Parameters
string:String

content to be used. Not null.

charset:Charset

character set to be used. May be null, in which case the default is StandardCharsets#ISO_8859_1 is assumed

Since
4.2
StringEntityback to summary
public StringEntity(final String string, final Charset charset, final boolean chunked)
StringEntityback to summary
public StringEntity(final String string)

Creates a StringEntity with the specified content. The content type defaults to ContentType#TEXT_PLAIN.

Parameters
string:String

content to be used. Not null.

Exceptions
IllegalArgumentException:
if the string parameter is null

Method Detail

closeback to summary
public final void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
getContentback to summary
public final InputStream getContent() throws IOException

Implements org.apache.hc.core5.http.HttpEntity.getContent.

Doc from org.apache.hc.core5.http.HttpEntity.getContent.

Returns a content stream of the entity. Repeatable entities are expected to create a new instance of InputStream for each invocation of this method and therefore can be consumed multiple times. Entities that are not repeatable are expected to return the same InputStream instance and therefore may not be consumed more than once.

If this entity belongs to an incoming HTTP message, calling InputStream#close() on the returned InputStream will try to consume the complete entity content to keep the connection alive. In cases where this is undesired, e.g. when only a small part of the content is relevant and consuming the complete entity content would be too inefficient, only the HTTP message from which this entity was obtained should be closed (if supported).

Important Note

Please note all entity implementations must ensure that all allocated resources are properly deallocated after the InputStream#close() method is invoked.

Returns:InputStream

content stream of the entity.

Annotations
@Override
Exceptions
IOException:
if the stream could not be created
getContentLengthback to summary
public final long getContentLength()

Implements org.apache.hc.core5.http.EntityDetails.getContentLength.

Doc from org.apache.hc.core5.http.EntityDetails.getContentLength.

Returns length of the entity, if known.

Annotations
@Override
isRepeatableback to summary
public final boolean isRepeatable()

Overrides org.apache.hc.core5.http.io.entity.AbstractHttpEntity.isRepeatable.

Implements org.apache.hc.core5.http.HttpEntity.isRepeatable.

Doc from org.apache.hc.core5.http.HttpEntity.isRepeatable.

Tells if the entity is capable of producing its data more than once. A repeatable entity's getContent() and writeTo(OutputStream) methods can be called more than once whereas a non-repeatable entity's can not.

Returns:boolean

true if the entity is repeatable, false otherwise.

Annotations
@Override
isStreamingback to summary
public final boolean isStreaming()

Implements org.apache.hc.core5.http.HttpEntity.isStreaming.

Doc from org.apache.hc.core5.http.HttpEntity.isStreaming.

Tells whether this entity depends on an underlying stream. Streamed entities that read data directly from the socket should return true. Self-contained entities should return false. Wrapping entities should delegate this call to the wrapped entity.

Returns:boolean

true if the entity content is streamed, false otherwise

Annotations
@Override
writeToback to summary
public final void writeTo(final OutputStream outStream) throws IOException

Overrides org.apache.hc.core5.http.io.entity.AbstractHttpEntity.writeTo.

Implements org.apache.hc.core5.http.HttpEntity.writeTo.

Doc from org.apache.hc.core5.http.HttpEntity.writeTo.

Writes the entity content out to the output stream.

Important Note

Please note all entity implementations must ensure that all allocated resources are properly deallocated when this method returns.

Parameters
outStream:OutputStream

the output stream to write entity content to

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs