3 import java.io.IOException;
7 * @author Jaroslav Tulach
9 final class SimpleBuffer implements Writer.ImplSeq {
10 private final Writer out;
11 private final StringBuffer sb = new StringBuffer();
13 public SimpleBuffer(Writer out) {
17 public void close() throws IOException {
22 public void flush() throws IOException {
23 if (sb.length() > 0) {
24 out.write(sb.toString());
30 public void write(CharSequence seq) throws IOException {
31 if (shouldBufferAsTheSequenceIsNotTooBig(seq)) {
39 /** At the end the purpose of BufferedWriter is to buffer writes, this
40 * method is here to decide when it is OK to prefer buffering and when
41 * it is better to delegate directly into the underlaying stream.
43 * @param csq the seqence to evaluate
44 * @return true if buffering from super class should be used
46 private static boolean shouldBufferAsTheSequenceIsNotTooBig(CharSequence csq) {
50 // as buffers are usually bigger than 1024, it makes sense to
51 // pay the penalty of converting the sequence to string, but buffering
53 if (csq.length() < 1024) {
56 // otherwise, just directly write down the char sequence