Skip to content

Commit 74746f7

Browse files
committed
Updates to resolve issues raised by Kevin Gilmore
1 parent a76d8a4 commit 74746f7

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

core-java-modules/core-java-9/src/test/java/com/baeldung/java9/inputstream/outputstream/InputStreamToOutputStreamUnitTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,74 +11,76 @@
1111
import com.google.common.io.ByteStreams;
1212

1313
public class InputStreamToOutputStreamUnitTest {
14-
15-
// buffer size used for reading and writing
16-
private static final int BUFFER_SIZE = 8192;
17-
14+
1815
/**
1916
* Reads all bytes from an input stream and writes them to an output stream.
2017
* @param source - input stream to copy data from
2118
* @param target - output stream to copy data too
2219
*/
23-
private static void copy(InputStream source, OutputStream target) throws IOException {
24-
byte[] buf = new byte[BUFFER_SIZE];
20+
void copy(InputStream source, OutputStream target) throws IOException {
21+
byte[] buf = new byte[8192];
2522
int length;
2623
while ((length = source.read(buf)) > 0) {
2724
target.write(buf, 0, length);
2825
}
2926
}
3027

3128
@Test
32-
public final void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
33-
final String initialString = "Hello World!";
29+
public void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
30+
String initialString = "Hello World!";
3431

3532
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
3633
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
3734
copy(inputStream, targetStream);
35+
3836
assertEquals(initialString, new String(targetStream.toByteArray()));
3937
}
4038
}
4139

4240
@Test
43-
public final void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException {
44-
final String initialString = randomAlphabetic(20480);
41+
public void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException {
42+
String initialString = randomAlphabetic(20480);
4543

4644
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
4745
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
4846
copy(inputStream, targetStream);
47+
4948
assertEquals(initialString, new String(targetStream.toByteArray()));
5049
}
5150
}
5251

5352
@Test
54-
public final void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
55-
final String initialString = "Hello World!";
53+
public void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
54+
String initialString = "Hello World!";
5655

5756
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
5857
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
5958
inputStream.transferTo(targetStream);
59+
6060
assertEquals(initialString, new String(targetStream.toByteArray()));
6161
}
6262
}
6363

6464
@Test
65-
public final void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
66-
final String initialString = "Hello World!";
65+
public void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
66+
String initialString = "Hello World!";
6767

6868
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
6969
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
7070
ByteStreams.copy(inputStream, targetStream);
71+
7172
assertEquals(initialString, new String(targetStream.toByteArray()));
7273
}
7374
}
7475

7576
@Test
76-
public final void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
77-
final String initialString = "Hello World!";
77+
public void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
78+
String initialString = "Hello World!";
7879

7980
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
8081
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
8182
IOUtils.copy(inputStream, targetStream);
83+
8284
assertEquals(initialString, new String(targetStream.toByteArray()));
8385
}
8486
}

0 commit comments

Comments
 (0)