Skip to content

Commit 9b16156

Browse files
authored
Merge pull request eugenp#3814 from iaforek/master
BAEL-1609 - CountingOutputStream
2 parents 234701a + 5ce09eb commit 9b16156

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎guava/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
<properties>
6161
<!-- util -->
62-
<guava.version>21.0</guava.version>
62+
<guava.version>24.0-jre</guava.version>
6363
<commons-lang3.version>3.5</commons-lang3.version>
6464
<commons-collections4.version>4.1</commons-collections4.version>
6565

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.guava;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
6+
import org.junit.Test;
7+
8+
import com.google.common.io.CountingOutputStream;
9+
10+
public class GuavaCountingOutputStreamTest {
11+
public static final int MAX = 5;
12+
13+
@Test(expected = RuntimeException.class)
14+
public void givenData_whenCountReachesLimit_thenThrowException() throws Exception {
15+
ByteArrayOutputStream out = new ByteArrayOutputStream();
16+
CountingOutputStream cos = new CountingOutputStream(out);
17+
18+
byte[] data = new byte[1024];
19+
ByteArrayInputStream in = new ByteArrayInputStream(data);
20+
21+
int b;
22+
while ((b = in.read()) != -1) {
23+
cos.write(b);
24+
if (cos.getCount() >= MAX) {
25+
throw new RuntimeException("Write limit reached");
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)