프로그래밍 언어/JAVA
JAVA 노드 스트림 & 보조 스트림
KYBee
2022. 7. 31. 15:40
노드 스트림
- I/O : Data의 입력과 출력 ⇒ input & output
- 데이터는 한 쪽에서 주고 한 쪽에서 받는 구조로 되어 있음
- 이 때 입력과 출력의 엔드 포인트를 노드라고 칭함
- 두 노드를 서로 연결하여 데이터를 전송할 수 있는 개념 : 스트림(Stream)
스트림
- 두 노드(엔드포인트)를 연결하여 데이터를 전송하는 개념
- 단방향 통신
- 하나의 스트림으로 입력과 출력을 같이 처리 불가능
노드의 종류
- 키보드
- 모니터
- 메모리
- 파일
- 데이터베이스
- 네트워크
노드 스트림의 종류
- 스트림을 통해 흐르는 데이터 타입에 따라서
- XXStream → 바이트를 처리
- XXer → 캐릭터를 처리
- 방향에 따라서
- InputStream, Reader → 입력
- OutputStream, Writer → 출력
- 노드 타입에 따라서
- 키보드, File → InputStream
- 모니터, File, ByteArray Pipe → OutputStream
- 키보드, File, CharArray, String, Pipe → Reader
- 모니터, File, CharArray, String, Pipe → Writer
InputStream Interface 의 주요 메서드
- read()
/**
* byte 하나를 읽어서 int로 반환한다. 더 이상 읽을 값이 없으면 -1을 리턴한다.
*/
public abstract int read() throws IOExcpeiton;
/**
* 데이터를 읽어서 b를 채우고 읽은 바이트의 개수를 리턴한다. 0이 리턴되면 더 이상 읽을 값이 없는 상태.
*/
public int read(byte b[]) throws IOException;
/**
* 최대 len만큼 데이터를 읽어서 b의 offset부터 b에 저장하고 읽은 바이트 개수를 리턴한다. 따라서 len+offset은 b의 크기 이하여야 한다.
*/
public int read(byte b[], int offset, int len) throws IOException;
- close()
/**
* 스트림을 종료해서 자원을 반납한다.
*/
public void close() throws IOException;
Reader Interface 주요 메서드
- read()
/**
* char 하나를 읽어서 int로 반환한다. 더 이상 읽을 값이 없으면 -1을 리턴한다.
*/
public int read() throws IOExcpeiton;
/**
* 데이터를 읽어서 cbuf를 채우고 읽은 문자의 개수를 리턴한다. 0이 리턴 되면 더 이상 읽을 값이 없는 상황이다.
*/
public int read(char cbuf[]) throws IOException;
/**
* 최대 len만큼 데이터를 읽어서 cbuf의 off부터 cbuf에 저장하고 읽은 char 개수를 리턴한다. 따라서 len+off는 cbuf의 크기 이하여야 한다.
*/
public int read(char cbuf[], int off, int len) throws IOException;
/**
* 데이터를 읽어서 target에 저장한다. target은 cbuf를 대체한다.
*/
public int read(java.nio.CharBuffer target) throws IOException;
- close()
/**
* 스트림을 종료해서 자원을 반납한다.
*/
public void close() throws IOException;
OutputStream Interface 의 주요 메서드
- write()
/**
* b의 내용을 byte로 출력한다.
*/
public abstract void write(int b) throws IOExcpeiton;
/**
* b를 문자열로 변환해서 출력한다.
*/
public void write(byte b[]) throws IOException;
/**
* b의 off 부터 off+len-1만큼을 문자열로 변환해서 출력한다.
*/
public void write(byte b[], int off, int len) throws IOException;
- close()
/**
* 스트림을 종료해서 자원을 반납한다. close() 는 내부적으로 flush() 를 호출한다.
*/
public void close() throws IOException;
- flush()
/**
* 버퍼가 있는 스트림에서 버퍼의 내용을 출력하고 버퍼를 비운다.
*/
public void flush() throws IOException;
Writer Interface 주요 메서드
- read()
/**
* b의 내용을 char로 출력한다.
*/
public void write(int c) throws IOExcpeiton;
/**
* cbuf를 문자열로 변환해서 출력한다.
*/
public void write(char cbuf[]) throws IOException;
/**
* cbuf의 off 부터 off + len - 1 만큼을 문자열로 변환해서 출력한다.
*/
abstract public void write(char cbuf[], int off, int len) throws IOException;
/**
* str을 출력한다.
*/
public void write(String str) throws IOException;
/**
* str의 off부터 off+len-1만큼을 출력한다.
*/
public void write(String str, int off, int len) throws IOException;
- append()
/**
* csq를 출력하고 Writer를 리턴한다.
*/
public Writer append(CharSequence csq) throws IOException;
/**
* csq의 start부터 end 까지를 출력하고 Writer를 리턴한다.
*/
public Writer append(CharSequence csq, int start, int end) throws IOException;
- close()
/**
* 스트림을 종료해서 자원을 반납한다. close() 는 내부적으로 flush() 를 호출한다.
*/
public void close() throws IOException;
- flush()
/**
* 버퍼가 있는 스트림에서 버퍼의 내용을 출력하고 버퍼를 비운다.
*/
public void flush() throws IOException;
보조 스트림
- 노드 스트림만 사용하는 경우는 많이 없음
- 보조 스트림 : Filter Stream, Processing Stream
- 다른 스트림에 부가적인 기능을 제공하는 스트림
- 스트림 체이닝 활용 가능 : 필요에 따라 여러 보조 스트림을 연결해서 사용하는 것
/**
* 생성은 이전 스트림을 생성자의 파라미터에 연결해서 사용한다.
*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- 보조 스트림을 close 하는 경우에 노드 스트림도 close 가 호출됨
- 보조 스트림 만으로는 스트림을 구성할 수 없음. 무조건 한 개의 노드 스트림 필요
보조 스트림 제공 기능 예시
- 문자 set 변환
- Buffering
- 기본 데이터 형의 전송
- 객체 입출력
종류
기능 | byte 기반 | char 기반 |
---|---|---|
byte 스트림을 char 스트림으로 변환 | InputStreamReader OutputStreamReader | |
버퍼링을 통한 속도 향상 | BufferedInputStream BufferedOutputStream | BufferedReader BufferedWriter |
객체 전송 | ObjectInputStream ObjectOutputStream |
사용할 스트림을 결정하는 과정
- 노드가 무엇인가?
- 타입은 문자열인가 바이트인가?
- 방향이 무엇인가?
- 추가 기능이 필요한가?
ex) 영화 파일을 빠른 속도로 이동시키려면?
- 노드는 File
- byte로
- 읽고, 쓴다 : FileInputStream, FileOutputStream
- 속도를 향상 시켜볼까? : BufferedInputStream, BufferedOutputStream