samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java
changeset 210 acf2c31e22d4
parent 209 1c999569643b
     1.1 --- a/samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java	Sat Jun 14 10:04:51 2008 +0200
     1.2 +++ b/samples/delegatingwriterfinal/src-api2.0/api/SimpleBuffer.java	Sat Jun 14 10:04:53 2008 +0200
     1.3 @@ -28,7 +28,7 @@
     1.4      }
     1.5  
     1.6      public void write(CharSequence seq) throws IOException {
     1.7 -        if (seq.length() < 1024) {
     1.8 +        if (shouldBufferAsTheSequenceIsNotTooBig(seq)) {
     1.9              sb.append(seq);
    1.10          } else {
    1.11              flush();
    1.12 @@ -36,4 +36,26 @@
    1.13          }
    1.14      }
    1.15  
    1.16 +    /** At the end the purpose of BufferedWriter is to buffer writes, this
    1.17 +     * method is here to decide when it is OK to prefer buffering and when 
    1.18 +     * it is better to delegate directly into the underlaying stream.
    1.19 +     * 
    1.20 +     * @param csq the seqence to evaluate
    1.21 +     * @return true if buffering from super class should be used
    1.22 +     */
    1.23 +    private static boolean shouldBufferAsTheSequenceIsNotTooBig(CharSequence csq) {
    1.24 +        if (csq == null) {
    1.25 +            return false;
    1.26 +        }
    1.27 +        // as buffers are usually bigger than 1024, it makes sense to 
    1.28 +        // pay the penalty of converting the sequence to string, but buffering
    1.29 +        // the write
    1.30 +        if (csq.length() < 1024) {
    1.31 +            return true;
    1.32 +        } else {
    1.33 +            // otherwise, just directly write down the char sequence
    1.34 +            return false;
    1.35 +        }
    1.36 +    }
    1.37 +    
    1.38  }