Class 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 sequence input --> 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 of n characters.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • _first

        final Encoder _first
        The first encoder to apply in sequence.
      • _last

        final Encoder _last
        The second encoder to apply in sequence.
      • _buffer

        final java.nio.CharBuffer _buffer
        The buffer used to store the output of the first encoder before sending as input to the second encoder.
    • Constructor Detail

      • ChainedEncoder

        ChainedEncoder​(Encoder first,
                       Encoder last)
        Creates an ChainedEncoder that applies the encoding sequence input --> first --> last --> output.
        Parameters:
        first - the first encoder to apply
        last - the second/last encoder to apply.
    • 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 class Encoder
        Parameters:
        input - the input to check for encoding
        off - the offset of the first character to check
        len - 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 of n characters.
        Specified by:
        maxEncodedLength in class Encoder
        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() 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());
         
        Overrides:
        encode in class Encoder
        Parameters:
        input - the input buffer to encode
        output - the output buffer to receive the encoded results
        endOfInput - 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.
        Returns:
        Either 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.
      • 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 class Encoder
        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 class java.lang.Object