package class5_3_StreamDecorator_inClass; import java.io.*; /** * The StarInserter is a FilterWriter that wraps * any Writer, printing a '*' before every character that it is * requested to print (except newlines) */ public class StarInserter extends FilterWriter { /** * Wrap out with this FilterWriter * @param out The writer to be wrapped. */ public StarInserter(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!='\r'&&c!='\n') { super.write('*'); } 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 { throw new UnsupportedEncodingException("Just hoping our client never calls this!"); } /** * 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 { for(int i = off; i