import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * Decorator: un Writer que envuelve a otro, le pasa todo lo que recibe y,
 * de paso, cuenta las líneas escritas.
 */
public class CountingWriter extends FilterWriter {
    private int lines = 0;

    public CountingWriter(Writer inner) {
        super(inner);
    }

    @Override
    public void write(int c) throws IOException {
        if (c == '\n') {
            lines++;
        }
        super.write(c);
    }

    @Override
    public void write(char[] buf, int off, int len) throws IOException {
        for (int i = off; i < off + len; i++) {
            if (buf[i] == '\n') {
                lines++;
            }
        }
        super.write(buf, off, len);
    }

    @Override
    public void write(String str, int off, int len) throws IOException {
        for (int i = off; i < off + len; i++) {
            if (str.charAt(i) == '\n') {
                lines++;
            }
        }
        super.write(str, off, len);
    }

    public int lines() {
        return lines;
    }
}
