diff -r 1c999569643b -r acf2c31e22d4 samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java --- a/samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java Sat Jun 14 10:04:51 2008 +0200 +++ b/samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java Sat Jun 14 10:04:53 2008 +0200 @@ -28,7 +28,7 @@ } public void write(CharSequence seq) throws IOException { - if (seq.length() < 1024) { + if (shouldBufferAsTheSequenceIsNotTooBig(seq)) { sb.append(seq); } else { flush(); @@ -36,4 +36,26 @@ } } + /** At the end the purpose of BufferedWriter is to buffer writes, this + * method is here to decide when it is OK to prefer buffering and when + * it is better to delegate directly into the underlaying stream. + * + * @param csq the seqence to evaluate + * @return true if buffering from super class should be used + */ + private static boolean shouldBufferAsTheSequenceIsNotTooBig(CharSequence csq) { + if (csq == null) { + return false; + } + // as buffers are usually bigger than 1024, it makes sense to + // pay the penalty of converting the sequence to string, but buffering + // the write + if (csq.length() < 1024) { + return true; + } else { + // otherwise, just directly write down the char sequence + return false; + } + } + }