1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.input;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.OutputStream;
21 import java.io.IOException;
22
23 import junit.framework.TestCase;
24 import org.apache.commons.io.IOUtils;
25 import org.apache.commons.io.output.NullOutputStream;
26
27
28
29
30
31
32
33
34 public class CountingInputStreamTest extends TestCase {
35
36 public CountingInputStreamTest(String name) {
37 super(name);
38 }
39
40 public void testCounting() throws Exception {
41 String text = "A piece of text";
42 byte[] bytes = text.getBytes();
43 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
44 CountingInputStream cis = new CountingInputStream(bais);
45
46
47
48
49 byte[] result = new byte[21];
50
51 byte[] ba = new byte[5];
52 int found = cis.read(ba);
53 System.arraycopy(ba, 0, result, 0, 5);
54 assertEquals( found, cis.getCount() );
55
56 int value = cis.read();
57 found++;
58 result[5] = (byte)value;
59 assertEquals( found, cis.getCount() );
60
61 found += cis.read(result, 6, 5);
62 assertEquals( found, cis.getCount() );
63
64 found += cis.read(result, 11, 10);
65 assertEquals( found, cis.getCount() );
66
67
68 String textResult = new String(result).trim();
69 assertEquals(textResult, text);
70 }
71
72
73
74
75
76 public void testLargeFiles_IO84() throws Exception {
77 long size = (long)Integer.MAX_VALUE + (long)1;
78 NullInputStream mock = new NullInputStream(size);
79 CountingInputStream cis = new CountingInputStream(mock);
80 OutputStream out = new NullOutputStream();
81
82
83 IOUtils.copyLarge(cis, out);
84 try {
85 cis.getCount();
86 fail("Expected getCount() to throw an ArithmeticException");
87 } catch (ArithmeticException ae) {
88
89 }
90 try {
91 cis.resetCount();
92 fail("Expected resetCount() to throw an ArithmeticException");
93 } catch (ArithmeticException ae) {
94
95 }
96
97 mock.close();
98
99
100 IOUtils.copyLarge(cis, out);
101 assertEquals("getByteCount()", size, cis.getByteCount());
102 assertEquals("resetByteCount()", size, cis.resetByteCount());
103 }
104
105 public void testResetting() throws Exception {
106 String text = "A piece of text";
107 byte[] bytes = text.getBytes();
108 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
109 CountingInputStream cis = new CountingInputStream(bais);
110
111 byte[] result = new byte[bytes.length];
112
113 int found = cis.read(result, 0, 5);
114 assertEquals( found, cis.getCount() );
115
116 int count = cis.resetCount();
117 found = cis.read(result, 6, 5);
118 assertEquals( found, count );
119 }
120
121 public void testZeroLength1() throws Exception {
122 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
123 CountingInputStream cis = new CountingInputStream(bais);
124
125 int found = cis.read();
126 assertEquals(-1, found);
127 assertEquals(0, cis.getCount());
128 }
129
130 public void testZeroLength2() throws Exception {
131 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
132 CountingInputStream cis = new CountingInputStream(bais);
133
134 byte[] result = new byte[10];
135
136 int found = cis.read(result);
137 assertEquals(-1, found);
138 assertEquals(0, cis.getCount());
139 }
140
141 public void testZeroLength3() throws Exception {
142 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
143 CountingInputStream cis = new CountingInputStream(bais);
144
145 byte[] result = new byte[10];
146
147 int found = cis.read(result, 0, 5);
148 assertEquals(-1, found);
149 assertEquals(0, cis.getCount());
150 }
151
152 public void testEOF1() throws Exception {
153 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
154 CountingInputStream cis = new CountingInputStream(bais);
155
156 int found = cis.read();
157 assertEquals(0, found);
158 assertEquals(1, cis.getCount());
159 found = cis.read();
160 assertEquals(0, found);
161 assertEquals(2, cis.getCount());
162 found = cis.read();
163 assertEquals(-1, found);
164 assertEquals(2, cis.getCount());
165 }
166
167 public void testEOF2() throws Exception {
168 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
169 CountingInputStream cis = new CountingInputStream(bais);
170
171 byte[] result = new byte[10];
172
173 int found = cis.read(result);
174 assertEquals(2, found);
175 assertEquals(2, cis.getCount());
176 }
177
178 public void testEOF3() throws Exception {
179 ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
180 CountingInputStream cis = new CountingInputStream(bais);
181
182 byte[] result = new byte[10];
183
184 int found = cis.read(result, 0, 5);
185 assertEquals(2, found);
186 assertEquals(2, cis.getCount());
187 }
188
189 public void testSkipping() throws IOException {
190 String text = "Hello World!";
191 byte[] bytes = text.getBytes();
192 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
193 CountingInputStream cis = new CountingInputStream(bais);
194
195 assertEquals(6,cis.skip(6));
196 assertEquals(6,cis.getCount());
197 final byte[] result = new byte[6];
198 cis.read(result);
199
200 assertEquals("World!",new String(result));
201 assertEquals(12,cis.getCount());
202 }
203
204 }