File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
java9-language-features-examples/src/test/java/com/howtoprogram/io Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments