All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class RegexOutputStreamextends FilterOutputStream
OutputStream that performs case-insensitive regex replacement during streaming.
  • Field Details

    • match

      private final String match
      Pattern string to match, lowercased.
    • repl

      private final String repl
      Replacement string.
    • buf

      private final StringBuilder buf
      Parse in progress.
    • noMatch

      private final String noMatch
      On-no-match fallback string.
    • _log

      private final Log _log
      Logger.
    • found

      private boolean found
      Whether a match was found.
    • idx

      int idx
      Match position within the pattern.
  • Constructor Details

    • RegexOutputStream

      public RegexOutputStream(OutputStream out, String pattern, String replace, String onNoMatch)
      Construct with a literal pattern that may contain a single '*' wildcard.
      Parameters:
      out - MUST be buffered because this writes one byte at a time
      pattern - the only special char recognized is '*' and cannot be at the beginning or end
      replace - the replacement string, ASCII-only, no UTF-8
      onNoMatch - force output of this at the end if no replacement made, or null
  • Method Details

    • write

      public void write(int val) throws IOException
      Write a single byte, with streaming pattern matching.
      Overrides:
      write in class FilterOutputStream
      Throws:
      IOException
    • pushit

      private void pushit(char c)
      Put in the pending parse buf.
      Parameters:
      c - the character to append to the parse buffer
    • flushit

      private void flushit() throws IOException
      Flush buf to out, start over.
      Throws:
      IOException - if writing to the underlying writer fails
    • replaceit

      private void replaceit() throws IOException
      Throw out inbuf, output replacement, start over.
      Throws:
      IOException - if writing the replacement to the underlying writer fails
    • logText

      static String logText(String s)
      Keeps a pattern, replacement or buffer readable in the log. These values are HTML fragments of several hundred characters, and one of them per replacement would bury the message the reader is scanning for.
      Parameters:
      s - the value to shorten, never null
      Returns:
      the value, or its head with an ellipsis when it is long
    • clearit

      private void clearit()
      Start over.
    • close

      public void close() throws IOException
      Flush pending output and close.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterOutputStream
      Throws:
      IOException