samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.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@67
     1
package api;
jtulach@67
     2
jtulach@67
     3
import java.io.IOException;
jtulach@67
     4
jtulach@67
     5
/**
jtulach@67
     6
 *
jtulach@67
     7
 * @author Jaroslav Tulach
jtulach@67
     8
 */
jtulach@67
     9
final class SimpleBuffer implements Writer.ImplSeq {
jtulach@67
    10
    private final Writer out;
jtulach@67
    11
    private final StringBuffer sb = new StringBuffer();
jtulach@67
    12
    
jtulach@67
    13
    public SimpleBuffer(Writer out) {
jtulach@67
    14
        this.out = out;
jtulach@67
    15
    }
jtulach@67
    16
jtulach@67
    17
    public void close() throws IOException {
jtulach@67
    18
        flush();
jtulach@67
    19
        out.close();
jtulach@67
    20
    }
jtulach@67
    21
jtulach@67
    22
    public void flush() throws IOException {
jtulach@67
    23
        if (sb.length() > 0) {
jtulach@67
    24
            out.write(sb.toString());
jtulach@67
    25
            sb.setLength(0);
jtulach@67
    26
            out.flush();
jtulach@67
    27
        }
jtulach@67
    28
    }
jtulach@67
    29
jtulach@67
    30
    public void write(CharSequence seq) throws IOException {
jtulach@210
    31
        if (shouldBufferAsTheSequenceIsNotTooBig(seq)) {
jtulach@67
    32
            sb.append(seq);
jtulach@67
    33
        } else {
jtulach@67
    34
            flush();
jtulach@67
    35
            out.append(seq);
jtulach@67
    36
        }
jtulach@67
    37
    }
jtulach@67
    38
jtulach@210
    39
    /** At the end the purpose of BufferedWriter is to buffer writes, this
jtulach@210
    40
     * method is here to decide when it is OK to prefer buffering and when 
jtulach@210
    41
     * it is better to delegate directly into the underlaying stream.
jtulach@210
    42
     * 
jtulach@210
    43
     * @param csq the seqence to evaluate
jtulach@210
    44
     * @return true if buffering from super class should be used
jtulach@210
    45
     */
jtulach@210
    46
    private static boolean shouldBufferAsTheSequenceIsNotTooBig(CharSequence csq) {
jtulach@210
    47
        if (csq == null) {
jtulach@210
    48
            return false;
jtulach@210
    49
        }
jtulach@210
    50
        // as buffers are usually bigger than 1024, it makes sense to 
jtulach@210
    51
        // pay the penalty of converting the sequence to string, but buffering
jtulach@210
    52
        // the write
jtulach@210
    53
        if (csq.length() < 1024) {
jtulach@210
    54
            return true;
jtulach@210
    55
        } else {
jtulach@210
    56
            // otherwise, just directly write down the char sequence
jtulach@210
    57
            return false;
jtulach@210
    58
        }
jtulach@210
    59
    }
jtulach@210
    60
    
jtulach@67
    61
}