
예시
package ex15; import java.io.IOException; import java.io.InputStream; public class StreamEx01 { public static void main(String[] args) { InputStream input = System.in; // 키보드로부터 입력받음 = Input try { int value = input.read(); System.out.println("받은 값 : " + value); } catch (IOException e) { throw new RuntimeException(e); } } }


컴퓨터 : 3가지 계층으로 나눔
Application > OS > HardWare
Application = 코드 : 프로그램 > 실행 : 프로세스
인간의 영역 → 인간이 이해하기 쉬운 Byte로 전송

Application 기준으로 Input, Output 구분
ex) Application 이 모니터로 Output = 출력
메모리에 데이터를 넣는 것 = 변수에 데이터를 넣는 것
ByteStream : 끝이 없는 Byte저장 공간
Byte의 흐름
1Byte 씩 전송됨
Stream이 없으면 OS로 데이터 전송이 안됨

임시로 만들어진 파일에 데이터가 들어옴
파일 : Application 과 OS의 매개체 역할을 하는 임시로 만들어진 파일
소켓 : 외부 세상에 데이터를 전송할 때
전송된 데이터는 누적되지 않고 OS가 소비할 때 지워짐
소비를 못하면 wait 걸림 → 출력이 밀려서 데이터가 쌓이고 결국 소멸됨
ex) 공급자가 데이터를 공급할 때 1초에 1개씩 전송할 때
1초에 1개씩 소비가 가능하면 상관없음
서로 같은 모니터를 공유해서 쓰기에 1초 만에 소비가 안되고
연속적으로 데이터가 와도 바빠서 6초 만에 소비할 수 있음
그럼 소멸될 수 있음
ex) 

ByteStream이라서 1Byte씩 읽고 버려진 것
동시에 두 개가 내려와도 os가 한 개만 소비할 수 있음
⇒ Buffer = 보조 Stream이 필요함
Buffer에 저장해야 소멸 안되고 꺼내 쓸 수 있음
Buffer가 필요할 때 : 공급이 소비보다 많을 때 필요함
버퍼링 : 소비가 공급보다 빠를 때, 소비가 공급보다 느릴 때 발생
Buffer의 특징 : 쓰기, 읽기
한 번 사용되고 난 후에는 다시 사용되지 않음
쓰기 Buffer : Output Stream = Output 버퍼
보조 Byte 몇 개인지는 개발자가 설정할 수 있음


데이터가 전송되는 과정

프로토콜 : 보조 Stream의 크기만큼 다 소비함
Buffer가 다 차지 않으면 소비하지 않음
flush : 다 차지 않았을 때 강제로 흘려보내는 것
전체 물길을 다 열어주는 것
통신의 기본 / 데이터 전송의 기본
보조 Stream이 Buffer가 되면 특이하게 작동함
통신할 때 사이즈를 고정해 놓으면 안됨
개발자 입장에서는 Buffer에 데이터를 담아서 다 전송하면 끝
프로세스마다 보조 thread가 만들어져서 실행이 됨
OS도 해당 Application 에 보조 Stream을 가지고 있음

읽기 Buffer : Input Buffer
ByteStream이 아님!!

Share article