public final class Entity extends Object implements Closeable
push()
, newDecodedContentReader(Charset)
, or
newDecodedContentInputStream()
. The branch can then be closed by
calling pop()
on the entity, or close()
on the returned
BufferedReader
or
InputStream
. Calling 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 | 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 |
getRawInputStream()
Returns an input stream representing the raw content of this entity.
|
String |
getString()
Returns the content of this entity decoded as a string.
|
boolean |
isEmpty()
Returns true if the entity 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
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 |
setJson(Object value)
Sets the content of this entity to the JSON representation of the
provided object.
|
void |
setRawInputStream(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 boolean isEmpty()
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 getRawInputStream()
public String getString() throws IOException
null
).IOException
- If an IO error occurred while reading the content.public InputStream newDecodedContentInputStream() throws UnsupportedEncodingException, 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 UnsupportedEncodingException, 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()
push()
.public void push() throws IOException
copyRawContentTo
or
#getRawInputStream()
, will not impact the saved state.
Use the 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) throws IOException
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.IOException
- If an IO error occurred while writing JSON, such as trying to
output content in wrong context (non-matching end-array or
end-object, for example).public void setRawInputStream(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 © 2014 ForgeRock AS. All rights reserved.