4
4
import org .junit .jupiter .api .Nested ;
5
5
import org .junit .jupiter .api .Test ;
6
6
7
- import java .io .*;
7
+ import java .io .BufferedInputStream ;
8
+ import java .io .BufferedOutputStream ;
9
+ import java .io .BufferedReader ;
10
+ import java .io .ByteArrayInputStream ;
11
+ import java .io .ByteArrayOutputStream ;
12
+ import java .io .FilterInputStream ;
13
+ import java .io .IOException ;
14
+ import java .io .InputStream ;
15
+ import java .io .InputStreamReader ;
16
+ import java .io .OutputStream ;
8
17
9
18
import static org .assertj .core .api .Assertions .assertThat ;
10
- import static org .mockito .Mockito .*;
19
+ import static org .mockito .Mockito .atLeastOnce ;
20
+ import static org .mockito .Mockito .mock ;
21
+ import static org .mockito .Mockito .verify ;
11
22
12
23
/**
13
24
* 자바는 스트림(Stream)으로부터 I/O를 사용한다.
14
25
* 입출력(I/O)은 하나의 시스템에서 다른 시스템으로 데이터를 이동 시킬 때 사용한다.
15
- *
26
+ * <p>
16
27
* InputStream은 데이터를 읽고, OutputStream은 데이터를 쓴다.
17
28
* FilterStream은 InputStream이나 OutputStream에 연결될 수 있다.
18
29
* FilterStream은 읽거나 쓰는 데이터를 수정할 때 사용한다. (e.g. 암호화, 압축, 포맷 변환)
19
- *
30
+ * <p>
20
31
* Stream은 데이터를 바이트로 읽고 쓴다.
21
32
* 바이트가 아닌 텍스트(문자)를 읽고 쓰려면 Reader와 Writer 클래스를 연결한다.
22
33
* Reader, Writer는 다양한 문자 인코딩(e.g. UTF-8)을 처리할 수 있다.
@@ -26,7 +37,7 @@ class IOStreamTest {
26
37
27
38
/**
28
39
* OutputStream 학습하기
29
- *
40
+ * <p>
30
41
* 자바의 기본 출력 클래스는 java.io.OutputStream이다.
31
42
* OutputStream의 write(int b) 메서드는 기반 메서드이다.
32
43
* <code>public abstract void write(int b) throws IOException;</code>
@@ -39,7 +50,7 @@ class OutputStream_학습_테스트 {
39
50
* OutputStream의 서브 클래스(subclass)는 특정 매체에 데이터를 쓰기 위해 write(int b) 메서드를 사용한다.
40
51
* 예를 들어, FilterOutputStream은 파일로 데이터를 쓸 때,
41
52
* 또는 DataOutputStream은 자바의 primitive type data를 다른 매체로 데이터를 쓸 때 사용한다.
42
- *
53
+ * <p>
43
54
* write 메서드는 데이터를 바이트로 출력하기 때문에 비효율적이다.
44
55
* <code>write(byte[] data)</code>와 <code>write(byte b[], int off, int len)</code> 메서드는
45
56
* 1바이트 이상을 한 번에 전송 할 수 있어 훨씬 효율적이다.
@@ -48,12 +59,8 @@ class OutputStream_학습_테스트 {
48
59
void OutputStream은_데이터를_바이트로_처리한다 () throws IOException {
49
60
final byte [] bytes = {110 , 101 , 120 , 116 , 115 , 116 , 101 , 112 };
50
61
final OutputStream outputStream = new ByteArrayOutputStream (bytes .length );
51
-
52
- /**
53
- * todo
54
- * OutputStream 객체의 write 메서드를 사용해서 테스트를 통과시킨다
55
- */
56
-
62
+
63
+ outputStream .write (bytes );
57
64
final String actual = outputStream .toString ();
58
65
59
66
assertThat (actual ).isEqualTo ("nextstep" );
@@ -63,7 +70,7 @@ class OutputStream_학습_테스트 {
63
70
/**
64
71
* 효율적인 전송을 위해 스트림에서 버퍼링을 사용 할 수 있다.
65
72
* BufferedOutputStream 필터를 연결하면 버퍼링이 가능하다.
66
- *
73
+ * <p>
67
74
* 버퍼링을 사용하면 OutputStream을 사용할 때 flush를 사용하자.
68
75
* flush() 메서드는 버퍼가 아직 가득 차지 않은 상황에서 강제로 버퍼의 내용을 전송한다.
69
76
* Stream은 동기(synchronous)로 동작하기 때문에 버퍼가 찰 때까지 기다리면
@@ -73,11 +80,7 @@ class OutputStream_학습_테스트 {
73
80
void BufferedOutputStream을_사용하면_버퍼링이_가능하다 () throws IOException {
74
81
final OutputStream outputStream = mock (BufferedOutputStream .class );
75
82
76
- /**
77
- * todo
78
- * flush를 사용해서 테스트를 통과시킨다.
79
- * ByteArrayOutputStream과 어떤 차이가 있을까?
80
- */
83
+ outputStream .flush ();
81
84
82
85
verify (outputStream , atLeastOnce ()).flush ();
83
86
outputStream .close ();
@@ -91,24 +94,22 @@ class OutputStream_학습_테스트 {
91
94
void OutputStream은_사용하고_나서_close_처리를_해준다 () throws IOException {
92
95
final OutputStream outputStream = mock (OutputStream .class );
93
96
94
- /**
95
- * todo
96
- * try-with-resources를 사용한다.
97
- * java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다.
98
- */
97
+ try (outputStream ) {
98
+
99
+ }
99
100
100
101
verify (outputStream , atLeastOnce ()).close ();
101
102
}
102
103
}
103
104
104
105
/**
105
106
* InputStream 학습하기
106
- *
107
+ * <p>
107
108
* 자바의 기본 입력 클래스는 java.io.InputStream이다.
108
109
* InputStream은 다른 매체로부터 바이트로 데이터를 읽을 때 사용한다.
109
110
* InputStream의 read() 메서드는 기반 메서드이다.
110
111
* <code>public abstract int read() throws IOException;</code>
111
- *
112
+ * <p>
112
113
* InputStream의 서브 클래스(subclass)는 특정 매체에 데이터를 읽기 위해 read() 메서드를 사용한다.
113
114
*/
114
115
@ Nested
@@ -124,11 +125,7 @@ class InputStream_학습_테스트 {
124
125
byte [] bytes = {-16 , -97 , -92 , -87 };
125
126
final InputStream inputStream = new ByteArrayInputStream (bytes );
126
127
127
- /**
128
- * todo
129
- * inputStream에서 바이트로 반환한 값을 문자열로 어떻게 바꿀까?
130
- */
131
- final String actual = "" ;
128
+ final String actual = new String (inputStream .readAllBytes ());
132
129
133
130
assertThat (actual ).isEqualTo ("🤩" );
134
131
assertThat (inputStream .read ()).isEqualTo (-1 );
@@ -143,19 +140,17 @@ class InputStream_학습_테스트 {
143
140
void InputStream은_사용하고_나서_close_처리를_해준다 () throws IOException {
144
141
final InputStream inputStream = mock (InputStream .class );
145
142
146
- /**
147
- * todo
148
- * try-with-resources를 사용한다.
149
- * java 9 이상에서는 변수를 try-with-resources로 처리할 수 있다.
150
- */
143
+ try (inputStream ) {
144
+
145
+ }
151
146
152
147
verify (inputStream , atLeastOnce ()).close ();
153
148
}
154
149
}
155
150
156
151
/**
157
152
* FilterStream 학습하기
158
- *
153
+ * <p>
159
154
* 필터는 필터 스트림, reader, writer로 나뉜다.
160
155
* 필터는 바이트를 다른 데이터 형식으로 변환 할 때 사용한다.
161
156
* reader, writer는 UTF-8, ISO 8859-1 같은 형식으로 인코딩된 텍스트를 처리하는 데 사용된다.
@@ -169,12 +164,12 @@ class FilterStream_학습_테스트 {
169
164
* 버퍼 크기를 지정하지 않으면 버퍼의 기본 사이즈는 얼마일까?
170
165
*/
171
166
@ Test
172
- void 필터인_BufferedInputStream를_사용해보자 () {
167
+ void 필터인_BufferedInputStream를_사용해보자 () throws IOException {
173
168
final String text = "필터에 연결해보자." ;
174
169
final InputStream inputStream = new ByteArrayInputStream (text .getBytes ());
175
- final InputStream bufferedInputStream = null ;
170
+ final InputStream bufferedInputStream = new BufferedInputStream ( inputStream ) ;
176
171
177
- final byte [] actual = new byte [ 0 ] ;
172
+ final byte [] actual = bufferedInputStream . readAllBytes () ;
178
173
179
174
assertThat (bufferedInputStream ).isInstanceOf (FilterInputStream .class );
180
175
assertThat (actual ).isEqualTo ("필터에 연결해보자." .getBytes ());
@@ -197,16 +192,23 @@ class InputStreamReader_학습_테스트 {
197
192
* 필터인 BufferedReader를 사용하면 readLine 메서드를 사용해서 문자열(String)을 한 줄 씩 읽어올 수 있다.
198
193
*/
199
194
@ Test
200
- void BufferedReader를_사용하여_문자열을_읽어온다 () {
195
+ void BufferedReader를_사용하여_문자열을_읽어온다 () throws IOException {
201
196
final String emoji = String .join ("\r \n " ,
202
197
"😀😃😄😁😆😅😂🤣🥲☺️😊" ,
203
198
"😇🙂🙃😉😌😍🥰😘😗😙😚" ,
204
199
"😋😛😝😜🤪🤨🧐🤓😎🥸🤩" ,
205
200
"" );
206
201
final InputStream inputStream = new ByteArrayInputStream (emoji .getBytes ());
202
+ final BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream ));
207
203
208
204
final StringBuilder actual = new StringBuilder ();
209
205
206
+ String line = bufferedReader .readLine ();
207
+ while (line != null ) {
208
+ actual .append (line + "\r \n " );
209
+ line = bufferedReader .readLine ();
210
+ }
211
+
210
212
assertThat (actual ).hasToString (emoji );
211
213
}
212
214
}
0 commit comments