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