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.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.ByteArrayInputStream;
24 import java.util.concurrent.atomic.AtomicBoolean;
25
26 import org.apache.commons.io.IOUtils;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32 public class BoundedInputStreamTest {
33
34 private void compare(final String msg, final byte[] expected, final byte[] actual) {
35 assertEquals(expected.length, actual.length, msg + " length");
36 for (int i = 0; i < expected.length; i++) {
37 assertEquals(expected[i], actual[i], msg + " byte[" + i + "]");
38 }
39 }
40
41 @Test
42 public void testOnMaxLength() throws Exception {
43 BoundedInputStream bounded;
44 final byte[] helloWorld = "Hello World".getBytes();
45 final byte[] hello = "Hello".getBytes();
46 final AtomicBoolean boolRef = new AtomicBoolean();
47
48
49 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length) {
50 @Override
51 protected void onMaxLength(final long max, final long readCount) {
52 boolRef.set(true);
53 }
54 };
55 assertFalse(boolRef.get());
56 for (int i = 0; i < helloWorld.length; i++) {
57 assertEquals(helloWorld[i], bounded.read(), "limit = length byte[" + i + "]");
58 }
59 assertEquals(-1, bounded.read(), "limit = length end");
60 assertTrue(boolRef.get());
61
62
63 boolRef.set(false);
64 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1) {
65 @Override
66 protected void onMaxLength(final long max, final long readCount) {
67 boolRef.set(true);
68 }
69 };
70 assertFalse(boolRef.get());
71 for (int i = 0; i < helloWorld.length; i++) {
72 assertEquals(helloWorld[i], bounded.read(), "limit > length byte[" + i + "]");
73 }
74 assertEquals(-1, bounded.read(), "limit > length end");
75 assertFalse(boolRef.get());
76
77
78 boolRef.set(false);
79 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), hello.length) {
80 @Override
81 protected void onMaxLength(final long max, final long readCount) {
82 boolRef.set(true);
83 }
84 };
85 assertFalse(boolRef.get());
86 for (int i = 0; i < hello.length; i++) {
87 assertEquals(hello[i], bounded.read(), "limit < length byte[" + i + "]");
88 }
89 assertEquals(-1, bounded.read(), "limit < length end");
90 assertTrue(boolRef.get());
91 }
92
93 @Test
94 public void testReadArray() throws Exception {
95
96 BoundedInputStream bounded;
97 final byte[] helloWorld = "Hello World".getBytes();
98 final byte[] hello = "Hello".getBytes();
99
100 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld));
101 compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded));
102
103 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), 0);
104 compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded));
105
106 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length);
107 compare("limit = length", helloWorld, IOUtils.toByteArray(bounded));
108
109 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1);
110 compare("limit > length", helloWorld, IOUtils.toByteArray(bounded));
111
112 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length - 6);
113 compare("limit < length", hello, IOUtils.toByteArray(bounded));
114 }
115
116 @Test
117 public void testReadSingle() throws Exception {
118 BoundedInputStream bounded;
119 final byte[] helloWorld = "Hello World".getBytes();
120 final byte[] hello = "Hello".getBytes();
121
122
123 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length);
124 for (int i = 0; i < helloWorld.length; i++) {
125 assertEquals(helloWorld[i], bounded.read(), "limit = length byte[" + i + "]");
126 }
127 assertEquals(-1, bounded.read(), "limit = length end");
128
129
130 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1);
131 for (int i = 0; i < helloWorld.length; i++) {
132 assertEquals(helloWorld[i], bounded.read(), "limit > length byte[" + i + "]");
133 }
134 assertEquals(-1, bounded.read(), "limit > length end");
135
136
137 bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), hello.length);
138 for (int i = 0; i < hello.length; i++) {
139 assertEquals(hello[i], bounded.read(), "limit < length byte[" + i + "]");
140 }
141 assertEquals(-1, bounded.read(), "limit < length end");
142 }
143 }