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

public Class StringEntity

extends AbstractHttpEntity
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable
Imports
java.io.ByteArrayInputStream, .IOException, .InputStream, .OutputStream, .UnsupportedEncodingException, java.nio.charset.Charset, .UnsupportedCharsetException, org.apache.http.protocol.HTTP, org.apache.http.util.Args

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

Field Summary

Modifier and TypeField and Description
protected final byte[]
Inherited from org.apache.http.entity.AbstractHttpEntity:
chunkedcontentEncodingcontentTypeOUTPUT_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
)

Creates a StringEntity with the specified content and content type.

public
StringEntity(final String
content to be used. Not null.
string
,
final String
MIME type to be used. May be null, in which case the default is HTTP#PLAIN_TEXT_TYPE i.e. "text/plain"
mimeType
,
final String
character set to be used. May be null, in which case the default is HTTP#DEF_CONTENT_CHARSET i.e. "ISO-8859-1"
charset
)

Deprecated (4.1.3) use StringEntity(String, ContentType)
Creates a StringEntity with the specified content, MIME type and charset
public
StringEntity(final String
content to be used. Not null.
string
,
final String
character set to be used. May be null, in which case the default is HTTP#DEF_CONTENT_CHARSET is assumed
charset
)

Creates a StringEntity with the specified content and charset.

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 HTTP#DEF_CONTENT_CHARSET is assumed
charset
)

Creates a StringEntity with the specified content and charset.

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 Object
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.
public InputStream
getContent()

Implements org.apache.http.HttpEntity.getContent.

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

Implements org.apache.http.HttpEntity.getContentLength.

Tells the length of the content, if known.
public boolean
isRepeatable()

Implements org.apache.http.HttpEntity.isRepeatable.

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

Returns:

false
isStreaming
()

Implements org.apache.http.HttpEntity.isStreaming.

Tells that this entity is not streaming.
public void
writeTo(final OutputStream
the output stream to write entity content to
outStream
)

Implements org.apache.http.HttpEntity.writeTo.

Writes the entity content out to the output stream.
Inherited from org.apache.http.entity.AbstractHttpEntity:
consumeContentgetContentEncodinggetContentTypeisChunkedsetChunkedsetContentEncodingsetContentEncodingsetContentTypesetContentTypetoString

Field Detail

contentback to summary
protected final byte[] content

Constructor Detail

StringEntityback to summary
public StringEntity(final String string, final ContentType contentType) throws UnsupportedCharsetException

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.

Exceptions
UnsupportedCharsetException:
Thrown when the named charset is not available in this instance of the Java virtual machine
IllegalArgumentException:
if the string parameter is null
Since
4.2
StringEntityback to summary
public StringEntity(final String string, final String mimeType, final String charset) throws UnsupportedEncodingException

Deprecated

(4.1.3) use StringEntity(String, ContentType)

Creates a StringEntity with the specified content, MIME type and charset

Parameters
string:String

content to be used. Not null.

mimeType:String

MIME type to be used. May be null, in which case the default is HTTP#PLAIN_TEXT_TYPE i.e. "text/plain"

charset:String

character set to be used. May be null, in which case the default is HTTP#DEF_CONTENT_CHARSET i.e. "ISO-8859-1"

Annotations
@Deprecated
Exceptions
UnsupportedEncodingException:
If the named charset is not supported.
IllegalArgumentException:
if the string parameter is null
Since
4.1
StringEntityback to summary
public StringEntity(final String string, final String charset) throws UnsupportedCharsetException

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:String

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

Exceptions
UnsupportedCharsetException:
Thrown when the named charset is not available in this instance of the Java virtual machine
IllegalArgumentException:
if the string parameter is null
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 HTTP#DEF_CONTENT_CHARSET is assumed

Exceptions
IllegalArgumentException:
if the string parameter is null
Since
4.2
StringEntityback to summary
public StringEntity(final String string) throws UnsupportedEncodingException

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
UnsupportedEncodingException:
if the default HTTP charset is not supported.
IllegalArgumentException:
if the string parameter is null

Method Detail

cloneback to summary
public Object clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Returns:Object

a clone of this instance.

Annotations
@Override
Exceptions
CloneNotSupportedException:
if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
getContentback to summary
public InputStream getContent() throws IOException

Implements org.apache.http.HttpEntity.getContent.

Doc from org.apache.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.

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 long getContentLength()

Implements org.apache.http.HttpEntity.getContentLength.

Doc from org.apache.http.HttpEntity.getContentLength.

Tells the length of the content, if known.

Returns:long

the number of bytes of the content, or a negative number if unknown. If the content length is known but exceeds Long.MAX_VALUE, a negative number is returned.

Annotations
@Override
isRepeatableback to summary
public boolean isRepeatable()

Implements org.apache.http.HttpEntity.isRepeatable.

Doc from org.apache.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 boolean isStreaming()

Implements org.apache.http.HttpEntity.isStreaming.

Tells that this entity is not streaming.

Returns:boolean

false

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

Implements org.apache.http.HttpEntity.writeTo.

Doc from org.apache.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