class ChainedEncoder extends Encoder
Modifier and Type | Field and Description |
---|---|
(package private) java.nio.CharBuffer |
_buffer
The buffer used to store the output of the first encoder before
sending as input to the second encoder.
|
(package private) Encoder |
_first
The first encoder to apply in sequence.
|
(package private) Encoder |
_last
The second encoder to apply in sequence.
|
Constructor and Description |
---|
ChainedEncoder(Encoder first,
Encoder last)
Creates an ChainedEncoder that applies the encoding sequence
input --> first --> last --> output . |
Modifier and Type | Method and Description |
---|---|
java.nio.charset.CoderResult |
encode(java.nio.CharBuffer input,
java.nio.CharBuffer output,
boolean endOfInput)
This is the kernel of encoding.
|
java.lang.String |
encode(java.lang.String str)
Encodes an input string to an output string.
|
protected java.nio.charset.CoderResult |
encodeArrays(java.nio.CharBuffer input,
java.nio.CharBuffer output,
boolean endOfInput)
The core encoding loop used when both the input and output buffers
are array backed.
|
protected int |
firstEncodedOffset(java.lang.String input,
int off,
int len)
Scans the input string for the first character index that requires
encoding.
|
protected int |
maxEncodedLength(int n)
Returns the maximum encoded length (in chars) of an input sequence of
n characters. |
java.lang.String |
toString() |
encodeBuffers, overflow, underflow
final Encoder _first
final Encoder _last
final java.nio.CharBuffer _buffer
public java.lang.String encode(java.lang.String str)
str
- the string to encodeprotected int firstEncodedOffset(java.lang.String input, int off, int len)
Encoder
firstEncodedOffset
in class Encoder
input
- the input to check for encodingoff
- the offset of the first character to checklen
- the number of characters to checkoff+len
if no characters in the input require encoding.protected int maxEncodedLength(int n)
Encoder
n
characters.maxEncodedLength
in class Encoder
n
- the number of characters of inputpublic java.nio.charset.CoderResult encode(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
Encoder
This is the kernel of encoding. Currently only CharBuffers
backed by arrays (i.e. CharBuffer.hasArray()
returns true
) are supported. Using a
direct-mapped CharBuffer will result in an
UnsupportedOperationException, though this behavior
may change in future releases.
This method should be called repeatedly while endOfInput
set to false
while there is more input.
Once there is no more input, this method should be called
endOfInput
set to false
until CoderResult.UNDERFLOW
is returned.
After any call to this method, except when endOfInput
is true
and the method returns UNDERFLOW
, there may be characters left to encode in the
input
buffer (i.e. input.hasRemaining() ==
true
). This will happen when the encoder needs to see more
input before determining what to do--for example when encoding
for CDATA, if the input ends with "foo]]"
, the encoder
will need to see the next character to determine if it is a ">"
or not.
Example usage:
CharBuffer input = CharBuffer.allocate(1024); CharBuffer output = CharBuffer.allocate(1024); CoderResult cr; // assuming doRead fills in the input buffer or // returns -1 at end of input while(doRead(input) != -1) { input.flip(); for (;;) { cr = encoder.encode(input, output, false); if (cr.isUnderflow()) { break; } if (cr.isOverflow()) { // assuming doWrite flushes the encoded // characters somewhere. output.flip(); doWrite(output); output.compact(); } } input.compact(); } // at end of input input.flip(); do { cr = encoder.encode(input, output, true); output.flip(); doWrite(output); output.compact(); } while (cr.isOverflow());
encode
in class Encoder
input
- the input buffer to encodeoutput
- the output buffer to receive the encoded resultsendOfInput
- set to true
if there is no more input, and any
remaining characters at the end of input will either be encoded or
replaced as invalid.CoderResult.UNDERFLOW
or CoderResult.OVERFLOW
. No other
CoderResult value will be returned. Characters or sequences
that might conceivably return and invalid or unmappable
character result (as part of the nio Charset API) are
automatically replaced to avoid security implications.protected java.nio.charset.CoderResult encodeArrays(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
Encoder
encodeArrays
in class Encoder
input
- the input buffer.output
- the output buffer.endOfInput
- when true, this is the last input to encodepublic java.lang.String toString()
toString
in class java.lang.Object