Class ChainedEncoder
- java.lang.Object
-
- org.owasp.encoder.Encoder
-
- org.owasp.encoder.ChainedEncoder
-
class ChainedEncoder extends Encoder
ChainedEncoder -- An encoder that chains together two encoders in order. This is included as an example, but not actually exposed or used as it requires an internal buffer making it not thread-safe. Sequences of 3 or more encodings require chaining together chained encoders.
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description ChainedEncoder(Encoder first, Encoder last)
Creates an ChainedEncoder that applies the encoding sequenceinput --> first --> last --> output
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
encode(java.lang.String str)
Encodes an input string to an output string.java.nio.charset.CoderResult
encode(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
This is the kernel of encoding.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 ofn
characters.java.lang.String
toString()
-
Methods inherited from class org.owasp.encoder.Encoder
encodeBuffers, overflow, underflow
-
-
-
-
Method Detail
-
encode
public java.lang.String encode(java.lang.String str)
Encodes an input string to an output string.- Parameters:
str
- the string to encode- Returns:
- the encoded string.
-
firstEncodedOffset
protected int firstEncodedOffset(java.lang.String input, int off, int len)
Description copied from class:Encoder
Scans the input string for the first character index that requires encoding. If the entire input does not require encoding then the length is returned. This method is used by the Encode.forXYZ methods to return input strings unchanged when possible.- Specified by:
firstEncodedOffset
in classEncoder
- Parameters:
input
- the input to check for encodingoff
- the offset of the first character to checklen
- the number of characters to check- Returns:
- the index of the first character to encode. The return value
will be
off+len
if no characters in the input require encoding.
-
maxEncodedLength
protected int maxEncodedLength(int n)
Description copied from class:Encoder
Returns the maximum encoded length (in chars) of an input sequence ofn
characters.- Specified by:
maxEncodedLength
in classEncoder
- Parameters:
n
- the number of characters of input- Returns:
- the worst-case number of characters required to encode
-
encode
public java.nio.charset.CoderResult encode(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
Description copied from class:Encoder
This is the kernel of encoding. Currently only CharBuffers backed by arrays (i.e.
CharBuffer.hasArray()
returnstrue
) 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 tofalse
while there is more input. Once there is no more input, this method should be calledendOfInput
set tofalse
untilCoderResult.UNDERFLOW
is returned.After any call to this method, except when
endOfInput
istrue
and the method returnsUNDERFLOW
, there may be characters left to encode in theinput
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());
- Overrides:
encode
in classEncoder
- Parameters:
input
- the input buffer to encodeoutput
- the output buffer to receive the encoded resultsendOfInput
- set totrue
if there is no more input, and any remaining characters at the end of input will either be encoded or replaced as invalid.- Returns:
- Either
CoderResult.UNDERFLOW
orCoderResult.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.
-
encodeArrays
protected java.nio.charset.CoderResult encodeArrays(java.nio.CharBuffer input, java.nio.CharBuffer output, boolean endOfInput)
Description copied from class:Encoder
The core encoding loop used when both the input and output buffers are array backed. The loop is expected to fetch the arrays and interact with the arrays directly for performance.- Overrides:
encodeArrays
in classEncoder
- Parameters:
input
- the input buffer.output
- the output buffer.endOfInput
- when true, this is the last input to encode- Returns:
- UNDERFLOW or OVERFLOW
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-