aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/text/GenericWriter.java
blob: cc867c00ee255825f13b88cbd91d1984d8f6a5d9 (plain) (blame)
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
68
69
70
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

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

/**
 * This is a basic writer for presenting text. Its has the pattern as
 * java.io.Writer, but it allows for more overrides for speed.
 * This introduces additional interfaces in addition to the java.lang.Writer.
 * The purpose is to allow for optimizations.
 *
 * @author baldersheim
 */
public abstract class GenericWriter extends Writer {

    public GenericWriter write(char c) throws java.io.IOException {
        char[] t = new char[1];
        t[0] = c;
        try {
            write(t, 0, 1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return this;
    }

    public GenericWriter write(CharSequence s) throws java.io.IOException {
        for (int i=0, m=s.length(); i < m; i++) {
            write(s.charAt(i));
        }
        return this;
    }

    public GenericWriter write(long i) throws java.io.IOException {
        write(String.valueOf(i));
        return this;
    }

    public GenericWriter write(short i) throws java.io.IOException {
        write(String.valueOf(i));
        return this;
    }

    public GenericWriter write(byte i) throws java.io.IOException {
        write(String.valueOf(i));
        return this;
    }

    public GenericWriter write(double i) throws java.io.IOException {
        write(String.valueOf(i));
        return this;
    }

    public GenericWriter write(float i) throws java.io.IOException {
        write(String.valueOf(i));
        return this;
    }

    public GenericWriter write(boolean i) throws java.io.IOException {
        write(String.valueOf(i));
        return this;
    }

    public GenericWriter write(AbstractUtf8Array v) throws java.io.IOException {
        write(v.toString());
        return this;
    }

}