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