Merge pull request #3814 from iaforek/master

BAEL-1609 - CountingOutputStream
This commit is contained in:
Tom Hombergs 2018-03-18 20:39:55 +01:00 committed by GitHub
commit 9b16156872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -59,7 +59,7 @@
<properties>
<!-- util -->
<guava.version>21.0</guava.version>
<guava.version>24.0-jre</guava.version>
<commons-lang3.version>3.5</commons-lang3.version>
<commons-collections4.version>4.1</commons-collections4.version>

View File

@ -0,0 +1,29 @@
package org.baeldung.guava;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.junit.Test;
import com.google.common.io.CountingOutputStream;
public class GuavaCountingOutputStreamTest {
public static final int MAX = 5;
@Test(expected = RuntimeException.class)
public void givenData_whenCountReachesLimit_thenThrowException() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
CountingOutputStream cos = new CountingOutputStream(out);
byte[] data = new byte[1024];
ByteArrayInputStream in = new ByteArrayInputStream(data);
int b;
while ((b = in.read()) != -1) {
cos.write(b);
if (cos.getCount() >= MAX) {
throw new RuntimeException("Write limit reached");
}
}
}
}