Class SubstitutingWriter

  • All Implemented Interfaces:
    Closeable, Flushable, Appendable, AutoCloseable
    Direct Known Subclasses:
    JsStringLiteralWriter

    public abstract class SubstitutingWriter
    extends FilterWriter

    An SubstitutingWriter is a FilterWriter that replaces each character in the output stream with the String obtained by calling the abstract method substitute on the characters.

    Characters for which substitute returns null are output verbatim.

    The implementation is fairly efficient in the number of calls to the various out.write methods.

    Author:
    Axel Wienberg
    • Constructor Detail

      • SubstitutingWriter

        public SubstitutingWriter​(Writer out)
    • Method Detail

      • substitute

        protected abstract String substitute​(char c)
        What should be printed instead of character c? This method is always called synchronized on out. Changes to the substitution function should therefore also be synchronized on out.
        Parameters:
        c - the character
        Returns:
        the string to be printed, or null for "no change", i.e. just c
      • write

        public void write​(int c)
                   throws IOException
        Write a single character, applying the substitution.
        Overrides:
        write in class FilterWriter
        Throws:
        IOException - If an I/O error occurs
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws IOException
        Write a portion of an array of characters, applying the substitution.
        Overrides:
        write in class FilterWriter
        Parameters:
        cbuf - Buffer of characters to be written
        off - Offset from which to start reading characters
        len - Number of characters to be written
        Throws:
        IOException - If an I/O error occurs
      • write

        public void write​(String str,
                          int off,
                          int len)
                   throws IOException
        Write a portion of a string, applying the substitution.
        Overrides:
        write in class FilterWriter
        Parameters:
        str - String to be written
        off - Offset from which to start reading characters
        len - Number of characters to be written
        Throws:
        IOException - If an I/O error occurs