samples/delegatingwriter/test/org/apidesign/delegatingwriter/CountingWriter.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:04:53 +0200
changeset 210 acf2c31e22d4
parent 209 1c999569643b
permissions -rw-r--r--
Merge: Geertjan's changes to the end of the chapter
     1 
     2 package org.apidesign.delegatingwriter;
     3 
     4 import java.io.IOException;
     5 import java.io.Writer;
     6 
     7 // BEGIN: writer.CountingWriter
     8 /** Writer that counts the number of written in characters.
     9  */
    10 public class CountingWriter extends Writer {
    11     private int counter;
    12     
    13     
    14     public int getCharacterCount() {
    15         return counter;
    16     }
    17 
    18     @Override
    19     public void write(char[] cbuf, int off, int len) throws IOException {
    20         counter += len;
    21     }
    22 
    23     @Override
    24     public Writer append(CharSequence csq) throws IOException {
    25         counter += csq.length();
    26         return this;
    27     }
    28 // FINISH: writer.CountingWriter
    29 
    30     @Override
    31     public Writer append(CharSequence csq, int start, int end) throws IOException {
    32         counter += (end - start);
    33         return this;
    34     }
    35 
    36     @Override
    37     public Writer append(char c) throws IOException {
    38         counter++;
    39         return this;
    40     }
    41 
    42     @Override
    43     public void write(int c) throws IOException {
    44         counter++;
    45     }
    46 
    47     @Override
    48     public void write(char[] cbuf) throws IOException {
    49         counter += cbuf.length;
    50     }
    51 
    52     @Override
    53     public void write(String str) throws IOException {
    54         counter += str.length();
    55     }
    56 
    57     @Override
    58     public void write(String str, int off, int len) throws IOException {
    59         counter += len;
    60     }
    61 
    62     @Override
    63     public void flush() throws IOException {
    64     }
    65 
    66     @Override
    67     public void close() throws IOException {
    68     }
    69 
    70 }