본문 바로가기
Java/stream

Java Stream - PipedInputStream & PipedOutputStream

by S.T.Lee 2022. 11. 13.
package lec08.fileio04.second.stream.q;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedStreamTest {
	
	public static void main(String[] args) {
		
		try {
			PipedInputStream pipedInputStream = new PipedInputStream();
			PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
			
			Thread oneThread = new Thread() {
				public void run() {
					int count = 0;
					try {
						while(true) {
							try {
								Thread.sleep(1000);
								String strData = "There is no easy day[" + count + "]";
								pipedOutputStream.write(strData.getBytes());
							} catch (Exception e) {
								e.printStackTrace();
							}
							count++;
						}
					} catch(Exception e) {
						e.printStackTrace();
					}
				}
			};
			oneThread.start();
			Thread twoThread = new Thread() {
					public void run() {
						byte[] bytes = new byte[1024];
						while (true) {
							try {
								Thread.sleep(1000);
								int readData = pipedInputStream.read(bytes);
								String strReadData = new String(bytes, 0, readData);
								
								System.out.println(strReadData);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				};
				twoThread.start();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

'Java > stream' 카테고리의 다른 글

Java Stream 예시 2  (0) 2022.11.13
Java Stream 예시 1  (0) 2022.11.13
Java Stream - NetStream(Url 읽어서 변환하기)  (0) 2022.11.13
Java Stream - BufferReader vs InputStreamReader  (0) 2022.11.13
Java Stream - OutputStreamWriter  (0) 2022.11.13