Skip to content

Commit 465374a

Browse files
committed
inputStream.transferTo in Java 9
1 parent 8906434 commit 465374a

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.howtoprogram.io;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.*;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class InputStreamTransferToTest {
10+
11+
@Test
12+
public void testTransferTo() throws IOException {
13+
14+
ByteArrayInputStream bis = new ByteArrayInputStream("Hello".getBytes());
15+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
16+
try {
17+
18+
19+
long count = bis.transferTo(bos);
20+
21+
byte[] bytes = bos.toByteArray();
22+
23+
assertEquals(count, bytes.length);
24+
25+
26+
} finally {
27+
try {
28+
bis.close();
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
33+
try {
34+
bos.close();
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
39+
40+
}
41+
42+
}
43+
44+
@Test
45+
public void testTransferToJava9Syntax() throws IOException {
46+
47+
ByteArrayInputStream bis = new ByteArrayInputStream("Hello".getBytes());
48+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
49+
50+
try (bis; bos) {
51+
52+
long count = bis.transferTo(bos);
53+
54+
byte[] bytes = bos.toByteArray();
55+
assertEquals(count, bytes.length);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)