Class EncodedWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class EncodedWriter
    extends java.io.Writer
    EncodedWriter -- A writer the encodes all input for a specific context and writes the encoded output to another writer.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.CharBuffer _buffer
      Where encoded output is buffered before sending on to the output writer.
      private Encoder _encoder
      The encoder used to encode input to the output writer.
      private boolean _hasLeftOver
      Some encoders require more input or an explicit end-of-input flag before they will process the remaining characters of an input buffer.
      private java.nio.CharBuffer _leftOverBuffer
      See comment on _hasLeftOver.
      private java.io.Writer _out
      The wrapped writer.
      (package private) static int BUFFER_SIZE
      Buffer size to allocate.
      (package private) static int LEFT_OVER_BUFFER
      Buffer to use for handling characters remaining in the input buffer after an encode.
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      EncodedWriter​(java.io.Writer out, java.lang.String contextName)
      Creates an EncodedWriter that uses the specified encoder to encode all input before sending it to the wrapped writer.
      EncodedWriter​(java.io.Writer out, Encoder encoder)
      Creates an EncodedWriter that uses the specified encoder to encode all input before sending it to the wrapped writer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      private void flushBufferToWriter()
      Flushes the contents of the buffer to the writer and resets the buffer to make room for more input.
      private void flushLeftOver​(java.nio.CharBuffer input)
      Flushes the left-over buffer.
      void write​(char[] cbuf, int off, int len)  
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, write, write, write, write
      • Methods inherited from class java.lang.Object

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

      • LEFT_OVER_BUFFER

        static final int LEFT_OVER_BUFFER
        Buffer to use for handling characters remaining in the input buffer after an encode. The value is set high enough to handle the lookaheads of all the encoders in the package.
        See Also:
        Constant Field Values
      • _out

        private java.io.Writer _out
        The wrapped writer.
      • _encoder

        private Encoder _encoder
        The encoder used to encode input to the output writer.
      • _buffer

        private java.nio.CharBuffer _buffer
        Where encoded output is buffered before sending on to the output writer.
      • _hasLeftOver

        private boolean _hasLeftOver
        Some encoders require more input or an explicit end-of-input flag before they will process the remaining characters of an input buffer. Because the writer API cannot pass this information on to the caller (e.g. by returning how many bytes were actually written), this writer implementation must buffer up the remaining characters between calls. The _hasLeftOver boolean is a flag used to indicate that there are left over characters in the buffer.
      • _leftOverBuffer

        private java.nio.CharBuffer _leftOverBuffer
        See comment on _hasLeftOver. This buffer is created on-demand once. Whether it has anything to flush is determined by the _hasLeftOver flag.
    • Constructor Detail

      • EncodedWriter

        public EncodedWriter​(java.io.Writer out,
                             Encoder encoder)
        Creates an EncodedWriter that uses the specified encoder to encode all input before sending it to the wrapped writer.
        Parameters:
        out - the target for all writes
        encoder - the encoder to use
      • EncodedWriter

        public EncodedWriter​(java.io.Writer out,
                             java.lang.String contextName)
                      throws UnsupportedContextException
        Creates an EncodedWriter that uses the specified encoder to encode all input before sending it to the wrapped writer. This method is equivalent to calling:
             new EncodedWriter(out, Encoders.forName(contextName));
         
        Parameters:
        out - the target for all writes
        contextName - the encoding context name.
        Throws:
        UnsupportedContextException - if the contextName is unrecognized or not supported.
    • Method Detail

      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • flushBufferToWriter

        private void flushBufferToWriter()
                                  throws java.io.IOException
        Flushes the contents of the buffer to the writer and resets the buffer to make room for more input.
        Throws:
        java.io.IOException - thrown by the wrapped output.
      • flushLeftOver

        private void flushLeftOver​(java.nio.CharBuffer input)
                            throws java.io.IOException
        Flushes the left-over buffer. Characters from the input buffer are used to add more data to the _leftOverBuffer in order to make the flush happen.
        Parameters:
        input - the next input to encode, or null if at end of file.
        Throws:
        java.io.IOException - from the underlying writer.
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer
        Throws:
        java.io.IOException