1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.utils;
20
21 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26
27 import org.junit.jupiter.api.Test;
28
29 class CountingStreamTest {
30
31 @Test
32 void testInput() throws Exception {
33
34
35 final ByteArrayInputStream bis = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 });
36 try (CountingInputStream i = new CountingInputStream(bis)) {
37 assertEquals(1, i.read());
38 assertEquals(1, i.getBytesRead());
39 byte[] b = new byte[2];
40 i.read(b);
41 assertEquals(3, i.getBytesRead());
42 assertArrayEquals(new byte[] { 2, 3 }, b);
43 b = new byte[3];
44 i.read(b, 1, 1);
45 assertArrayEquals(new byte[] { 0, 4, 0 }, b);
46 assertEquals(4, i.getBytesRead());
47 i.count(-1);
48 assertEquals(4, i.getBytesRead());
49 i.count(-2);
50 assertEquals(2, i.getBytesRead());
51 }
52 }
53
54 @Test
55 void testOutput() throws Exception {
56
57
58 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
59 try (CountingOutputStream o = new CountingOutputStream(bos)) {
60 o.write(1);
61 assertEquals(1, o.getBytesWritten());
62 o.write(new byte[] { 2, 3 });
63 assertEquals(3, o.getBytesWritten());
64 o.write(new byte[] { 2, 3, 4, 5, }, 2, 1);
65 assertEquals(4, o.getBytesWritten());
66 o.count(-1);
67 assertEquals(4, o.getBytesWritten());
68 o.count(-2);
69 assertEquals(2, o.getBytesWritten());
70 }
71 assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bos.toByteArray());
72 }
73
74 }