summaryrefslogtreecommitdiff
path: root/tutorial/examples/decorator/ReturnSpecifier.java
blob: 031958bddd648a7601c533eeb006967bd1db992d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package examples.decorator;


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


/*
 * Here assumes there's no implementation in Writer
 * like 'implements Writer'
 */
public class ReturnSpecifier
    extends Writer
{
    private static final char[] mark = { ' ', '<' };

    private Writer out;

    public ReturnSpecifier(Writer out) {
    this.out = out;
    }

    public void write(char cbuf[], int off, int len)
    throws IOException
    {
    int done = off;
    for (int i = done; i < off + len; ++i) {
        if (cbuf[i] == Character.LINE_SEPARATOR
        || cbuf[i] == '\n')
        {
        out.write(cbuf, done, i - done);
        // print a mark for carriage return
        out.write(mark, 0, mark.length);
        // print a carriage return
        out.write(cbuf, i, 1);
        done = i + 1;
        }
    }
    out.write(cbuf, done, off + len - done);
    }

    public void close() throws IOException {
    out.close();
    }

    public void flush() throws IOException {
    out.flush();
    }

/*****
    public void write(char[] cbuf) throws IOException {
    out.write(cbuf);
    }

    public void write(int c) throws IOException {
    out.write(c);
    }

    public void write(String str) throws IOException {
    out.write(str);
    }

    public void write(String str, int off, int len) throws IOException {
    out.write(str, off, len);
    }
*****/
}