public final class Entity extends Object implements Closeable
Entity.push()
, Entity.newDecodedContentReader(Charset)
, or
Entity.newDecodedContentInputStream()
. The branch can then be closed by
calling Entity.pop()
on the entity, or close()
on the returned
BufferedReader
or
InputStream
. Calling Entity.close()
on the entity fully closes the input stream invaliding any branches in the
process.
Several convenience methods are provided for accessing the entity as either
byte
, string
, or JSON
content.
Modifier and Type | Field and Description |
---|---|
static String |
APPLICATION_JSON_CHARSET_UTF_8
The Content-Type used when setting the entity to JSON.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes all resources associated with this entity.
|
void |
copyDecodedContentTo(OutputStream out)
Copies the decoded content of this entity to the provided writer.
|
void |
copyDecodedContentTo(Writer out)
Copies the decoded content of this entity to the provided writer.
|
void |
copyRawContentTo(OutputStream out)
Copies the raw content of this entity to the provided output stream.
|
byte[] |
getBytes()
Returns a byte array containing a copy of the decoded content of this
entity.
|
Object |
getJson()
Returns the content of this entity decoded as a JSON object.
|
InputStream |
getRawContentInputStream()
Returns an input stream representing the raw content of this entity.
|
String |
getString()
Returns the content of this entity decoded as a string.
|
boolean |
isDecodedContentEmpty()
Returns
true if this entity's decoded content is empty. |
boolean |
isRawContentEmpty()
Returns
true if this entity's raw content is empty. |
InputStream |
newDecodedContentInputStream()
Returns a branched input stream representing the decoded content of this
entity.
|
BufferedReader |
newDecodedContentReader(Charset charset)
Returns a branched reader representing the decoded content of this
entity.
|
void |
pop()
Restores the underlying input stream to the state it had immediately
before the last call to
Entity.push() . |
void |
push()
Saves the current position of the underlying input stream and creates a
new buffered input stream.
|
void |
setBytes(byte[] value)
Sets the content of this entity to the raw data contained in the provided
byte array.
|
void |
setEmpty()
Mark this entity as being empty.
|
void |
setJson(Object value)
Sets the content of this entity to the JSON representation of the
provided object.
|
void |
setRawContentInputStream(BranchingInputStream is)
Sets the content of this entity to the provided input stream.
|
void |
setString(String value)
Sets the content of this entity to the provided string.
|
String |
toString()
Returns the content of this entity decoded as a string.
|
public static final String APPLICATION_JSON_CHARSET_UTF_8
public boolean isRawContentEmpty()
true
if this entity's raw content is empty.true
if this entity's raw content is empty.public boolean isDecodedContentEmpty()
true
if this entity's decoded content is empty.true
if this entity's decoded content is empty.public void setEmpty()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public void copyDecodedContentTo(OutputStream out) throws IOException
Content-Encoding
header if it
is present in the message.out
- The destination writer.IOException
- If an IO error occurred while copying the decoded content.public void copyDecodedContentTo(Writer out) throws IOException
Content-Encoding
and
Content-Type
headers if they are present in the message.out
- The destination writer.IOException
- If an IO error occurred while copying the decoded content.public void copyRawContentTo(OutputStream out) throws IOException
out
- The destination output stream.IOException
- If an IO error occurred while copying the raw content.public byte[] getBytes() throws IOException
null
).IOException
- If an IO error occurred while reading the content.public Object getJson() throws IOException
null
only if the content represents the JSON
null
value.IOException
- If an IO error occurred while reading the content or if the
JSON is malformed.public InputStream getRawContentInputStream()
public String getString() throws IOException
null
).IOException
- If an IO error occurred while reading the content.public InputStream newDecodedContentInputStream() throws IOException
The entity will be decompressed based on any codings that are specified
in the Content-Encoding
header.
Note: The caller is responsible for calling the input stream's
close
method when it is finished reading the entity.
UnsupportedEncodingException
- If content encoding are not supported.IOException
- If an IO error occurred while reading the content.public BufferedReader newDecodedContentReader(Charset charset) throws IOException
The entity will be decoded and/or decompressed based on any codings that
are specified in the Content-Encoding
header.
If charset
is not null
then it will be used to decode the
entity, otherwise the character set specified in the message's
Content-Type
header (if present) will be used, otherwise the
default ISO-8859-1
character set.
Note: The caller is responsible for calling the reader's
close
method when it is finished reading the entity.
charset
- The character set to decode with, or message-specified or
default if null
.UnsupportedEncodingException
- If content encoding or charset are not supported.IOException
- If an IO error occurred while reading the content.public void pop()
Entity.push()
.public void push() throws IOException
copyRawContentTo
or
#getRawInputStream()
, will not impact the saved state.
Use the Entity.pop()
method to restore the entity to the previous state
it had before this method was called.
IOException
- If this entity has been closed.public void setBytes(byte[] value)
Content-Length
header,
overwriting any existing header.
Note: This method does not attempt to encode the entity based-on any
codings specified in the Content-Encoding
header.
value
- A byte array containing the raw data.public void setJson(Object value)
Content-Type
and
Content-Length
headers, overwriting any existing header.
Note: This method does not attempt to encode the entity based-on any
codings specified in the Content-Encoding
header.
value
- The object whose JSON representation is to be store in this
entity.public void setRawContentInputStream(BranchingInputStream is)
is
- The input stream.public void setString(String value)
Content-Length
header, overwriting any existing header.
The character set specified in the message's Content-Type
header
(if present) will be used, otherwise the default ISO-8859-1
character set.
Note: This method does not attempt to encode the entity based-on any
codings specified in the Content-Encoding
header.
value
- The string whose value is to be store in this entity.Copyright © 2010-2018, ForgeRock All Rights Reserved.