package class5_3_StreamDecorator_inClass; import java.io.FilterWriter; import java.io.IOException; import java.io.Writer; /** * The StarInserter is a FilterWriter that wraps * any Writer, printing a '*' before every character that it is * requested to print (except newlines) */ public class StarInserterWithDebug extends FilterWriter { /** * If set to true, prints debugging information to System.err (could be trouble if we tried to use System.out!) */ public static final boolean DEBUG_ENABLED = false; /** * Wrap out with this FilterWriter * @param out The writer to be wrapped. */ public StarInserterWithDebug(Writer out) { super(out); } /** * Where the fun happens. * If the character is NOT a new line, we print it, but also print a star. * * All FilterWriters are required to implement this method to guarantee correctness. * @param c The character to be "enhanced" * @throws IOException */ @Override public void write(int c) throws IOException { if(c != '\n' && c!= '\r') { int c2 = '*'; writeToSuper(c2); } writeToSuper(c); } /** * A simple wrapper around a call to super.write() * that also prints debug information if the compile-time * DEBUG_ENABLED is set. * @param c The character to be written and possibly debugged. * @throws IOException */ private void writeToSuper(int c) throws IOException { if(DEBUG_ENABLED) { System.err.println("DEBUG: write(" + c + ")"); } super.write(c); } /** * All FilterWriters are required to implement this method to * guarantee correctness. * @param cbuf char array to be written to the underlying stream * @param off first char from cbuf to be written * @param len number of chars to be written * @throws IOException if the underlying stream does */ @Override public void write(char[] cbuf, int off, int len) throws IOException { if(DEBUG_ENABLED) { System.err.println("DEBUG: write(cbuf,off,len)"); } super.write(cbuf, off, len); } /** * All FilterWriters are required to implement this method to * guarantee correctness. * @param str String to be written to the underlying stream * @param off first char from str to be written * @param len number of chars to be written * @throws IOException if the underlying stream does ] */ @Override public void write(String str, int off, int len) throws IOException { if(DEBUG_ENABLED) { System.err.println("DEBUG: write(str, off, len)"); } for(int i = off; i