samples/delegatingwriter/src/org/apidesign/delegatingwriter/ExBufferedWriter.java
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package org.apidesign.delegatingwriter;
8 import java.io.BufferedWriter;
9 import java.io.IOException;
10 import java.io.Writer;
13 * This is a regular {@link BufferedWriter}, just it optimizes for
14 * huge {@link CharSequence} objects set to its {@link #append} method.
15 * Instead of converting them to string, which might required too much
16 * temporary memory, it delegates it directly.
18 * This class is <q>simulating</q> would would happen if JDK's BufferedWriter
20 * was designed to delegate its newly added <code>append</code> methods
21 * instead of calling its already existing <code>write</code> ones.
23 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
25 public class ExBufferedWriter extends BufferedWriter {
26 private final Writer out;
28 public ExBufferedWriter(Writer out, int sz, boolean optimized) {
30 this.out = optimized ? out : null;
34 public Writer append(CharSequence csq) throws IOException {
36 // efficient, yet dangerous delegation skipping methods known to
37 // subclasses that used version 1.4
41 // non-efficient variant of delegating via converting to String first
42 // and using one of methods that existed in 1.4
46 write(csq.toString());