samples/delegatingwriterfinal/src-test2.0/api/usage/twodotzero/BufferedWriterOnCDImageTest.java
Tests rewritten to new version, just the Writer version 2.0 does not yet implement Appendable
2 package api.usage.twodotzero;
5 import java.io.IOException;
6 import java.util.concurrent.atomic.AtomicInteger;
8 import static org.junit.Assert.*;
10 /** Emulates what goes wrong when one wants to process really large character
13 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
15 public class BufferedWriterOnCDImageTest {
17 public BufferedWriterOnCDImageTest() {
21 public void testBehaviourOfRealBufferInJDKObviouslyThisIsGoingToThrowOutOfMemoryError() throws IOException {
22 AtomicInteger cnt = new AtomicInteger();
23 Writer writer = CountingWriter.create(cnt);
24 CDSequence cdImage = new CDSequence();
25 Writer bufferedWriter = Writer.createBuffered(writer);
26 bufferedWriter.append(cdImage);
27 assertEquals("Correct number of writes delegated", cdImage.length(), cnt.get());
31 /** A "lazy" sequence of characters, for example one that can represent
32 * content of a CD, read it lazily, do not fit all into memory at once.
34 private static final class CDSequence implements CharSequence {
35 private final int start;
36 private final int end;
39 this(0, 647 * 1024 * 1024);
42 private CDSequence(int start, int end) {
52 public char charAt(int index) {
53 int ch = index % ('Z' - 'A' + 1);
54 return (char) ('A' + ch);
57 public CharSequence subSequence(int start, int end) {
58 return new CDSequence(start, end);
62 public String toString() {
63 char[] arr = new char[length()];
64 for (int i = 0; i < length(); i++) {
67 return new String(arr);
71 } // end of CharSequence