samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/BufferedWriterOnCDImageTest.java
changeset 66 8379bb7c0dff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/BufferedWriterOnCDImageTest.java	Sat Jun 14 09:53:06 2008 +0200
     1.3 @@ -0,0 +1,72 @@
     1.4 +
     1.5 +package api.usage.twodotzero;
     1.6 +
     1.7 +import api.Writer;
     1.8 +import java.io.IOException;
     1.9 +import java.util.concurrent.atomic.AtomicInteger;
    1.10 +import org.junit.Test;
    1.11 +import static org.junit.Assert.*;
    1.12 +
    1.13 +/** Emulates what goes wrong when one wants to process really large character 
    1.14 + * sequence.
    1.15 + *
    1.16 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.17 + */
    1.18 +public class BufferedWriterOnCDImageTest {
    1.19 +    
    1.20 +    public BufferedWriterOnCDImageTest() {
    1.21 +    }
    1.22 +
    1.23 +    @Test
    1.24 +    public void testBehaviourOfRealBufferInJDKObviouslyThisIsGoingToThrowOutOfMemoryError() throws IOException {
    1.25 +        AtomicInteger cnt = new AtomicInteger();
    1.26 +        Writer writer = CountingWriter.create(cnt);
    1.27 +        CDSequence cdImage = new CDSequence();
    1.28 +        Writer bufferedWriter = Writer.createBuffered(writer);
    1.29 +        bufferedWriter.append(cdImage);
    1.30 +        assertEquals("Correct number of writes delegated", cdImage.length(), cnt.get());
    1.31 +    }
    1.32 +
    1.33 +
    1.34 +    /** A "lazy" sequence of characters, for example one that can represent
    1.35 +     * content of a CD, read it lazily, do not fit all into memory at once.
    1.36 +     */
    1.37 +    private static final class CDSequence implements CharSequence {
    1.38 +        private final int start;
    1.39 +        private final int end;
    1.40 +        
    1.41 +        public CDSequence() {
    1.42 +            this(0, 647 * 1024 * 1024);
    1.43 +        }
    1.44 +
    1.45 +        private CDSequence(int start, int end) {
    1.46 +            this.start = start;
    1.47 +            this.end = end;
    1.48 +        }
    1.49 +        
    1.50 +        
    1.51 +        public int length() {
    1.52 +            return end - start;
    1.53 +        }
    1.54 +
    1.55 +        public char charAt(int index) {
    1.56 +            int ch = index % ('Z' - 'A' + 1);
    1.57 +            return (char) ('A' + ch);
    1.58 +        }
    1.59 +
    1.60 +        public CharSequence subSequence(int start, int end) {
    1.61 +            return new CDSequence(start, end);
    1.62 +        }
    1.63 +
    1.64 +        @Override
    1.65 +        public String toString() {
    1.66 +            char[] arr = new char[length()];
    1.67 +            for (int i = 0; i < length(); i++) {
    1.68 +                arr[i] = charAt(i);
    1.69 +            }
    1.70 +            return new String(arr);
    1.71 +        }
    1.72 +        
    1.73 +        
    1.74 +    } // end of CharSequence
    1.75 +}
    1.76 \ No newline at end of file