jtulach@61
|
1 |
|
jtulach@61
|
2 |
package org.apidesign.delegatingwriter;
|
jtulach@61
|
3 |
|
jtulach@61
|
4 |
import java.io.BufferedWriter;
|
jtulach@61
|
5 |
import java.io.IOException;
|
jtulach@61
|
6 |
import org.junit.Test;
|
jtulach@61
|
7 |
import static org.junit.Assert.*;
|
jtulach@61
|
8 |
|
jtulach@61
|
9 |
/** Emulates what goes wrong when one wants to process really large character
|
jtulach@61
|
10 |
* sequence.
|
jtulach@61
|
11 |
*
|
jtulach@61
|
12 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@61
|
13 |
*/
|
jtulach@61
|
14 |
public class BufferedWriterOnCDImageTest {
|
jtulach@61
|
15 |
|
jtulach@61
|
16 |
public BufferedWriterOnCDImageTest() {
|
jtulach@61
|
17 |
}
|
jtulach@61
|
18 |
|
jtulach@61
|
19 |
@Test
|
jtulach@61
|
20 |
public void testBehaviourOfRealBufferInJDKObviouslyThisIsGoingToThrowOutOfMemoryException() throws IOException {
|
jtulach@61
|
21 |
// BEGIN: writer.countcd
|
jtulach@61
|
22 |
CountingWriter writer = new CountingWriter();
|
jtulach@61
|
23 |
CDSequence cdImage = new CDSequence();
|
jtulach@61
|
24 |
BufferedWriter bufferedWriter = new BufferedWriter(writer);
|
jtulach@61
|
25 |
bufferedWriter.append(cdImage);
|
jtulach@61
|
26 |
assertEquals("Correct number of writes delegated", cdImage.length(), writer.getCharacterCount());
|
jtulach@61
|
27 |
// END: writer.countcd
|
jtulach@61
|
28 |
}
|
jtulach@61
|
29 |
|
jtulach@61
|
30 |
@Test
|
jtulach@61
|
31 |
public void testBehaviourOfBufferThatDelegatesToAppend() throws IOException {
|
jtulach@61
|
32 |
CountingWriter writer = new CountingWriter();
|
jtulach@61
|
33 |
CDSequence cdImage = new CDSequence();
|
jtulach@61
|
34 |
BufferedWriter bufferedWriter = new AltBufferedWriter(writer, AltBufferedWriter.Behaviour.DELEGATE_TO_OUT);
|
jtulach@61
|
35 |
bufferedWriter.append(cdImage);
|
jtulach@61
|
36 |
assertEquals("Correct number of writes delegated", cdImage.length(), writer.getCharacterCount());
|
jtulach@61
|
37 |
}
|
jtulach@61
|
38 |
|
jtulach@61
|
39 |
// BEGIN: writer.bigseq
|
jtulach@61
|
40 |
/** A "lazy" sequence of characters, for example one that can represent
|
jtulach@61
|
41 |
* content of a CD, read it lazily, do not fit all into memory at once.
|
jtulach@61
|
42 |
*/
|
jtulach@61
|
43 |
private static final class CDSequence implements CharSequence {
|
jtulach@61
|
44 |
private final int start;
|
jtulach@61
|
45 |
private final int end;
|
jtulach@61
|
46 |
|
jtulach@61
|
47 |
public CDSequence() {
|
jtulach@61
|
48 |
this(0, 647 * 1024 * 1024);
|
jtulach@61
|
49 |
}
|
jtulach@61
|
50 |
|
jtulach@61
|
51 |
private CDSequence(int start, int end) {
|
jtulach@61
|
52 |
this.start = start;
|
jtulach@61
|
53 |
this.end = end;
|
jtulach@61
|
54 |
}
|
jtulach@61
|
55 |
|
jtulach@61
|
56 |
|
jtulach@61
|
57 |
public int length() {
|
jtulach@61
|
58 |
return end - start;
|
jtulach@61
|
59 |
}
|
jtulach@61
|
60 |
// END: writer.bigseq
|
jtulach@61
|
61 |
|
jtulach@61
|
62 |
public char charAt(int index) {
|
jtulach@61
|
63 |
int ch = index % ('Z' - 'A' + 1);
|
jtulach@61
|
64 |
return (char) ('A' + ch);
|
jtulach@61
|
65 |
}
|
jtulach@61
|
66 |
|
jtulach@61
|
67 |
public CharSequence subSequence(int start, int end) {
|
jtulach@61
|
68 |
return new CDSequence(start, end);
|
jtulach@61
|
69 |
}
|
jtulach@61
|
70 |
|
jtulach@61
|
71 |
@Override
|
jtulach@61
|
72 |
public String toString() {
|
jtulach@61
|
73 |
char[] arr = new char[length()];
|
jtulach@61
|
74 |
for (int i = 0; i < length(); i++) {
|
jtulach@61
|
75 |
arr[i] = charAt(i);
|
jtulach@61
|
76 |
}
|
jtulach@61
|
77 |
return new String(arr);
|
jtulach@61
|
78 |
}
|
jtulach@61
|
79 |
|
jtulach@61
|
80 |
|
jtulach@61
|
81 |
} // end of CharSequence
|
jtulach@61
|
82 |
} |