![]() |
Java NIO: Pipe 내부 |
Creating a Pipe
Pipe.open() 메소드를 호출하여 Pipe를 연다. 다음 보는 방법이다:
Pipe pipe = Pipe.open();
Writing to a Pipe
Pipe에 기록하기 위해 싱크 채널에 접근해야만 한다. 다음 수행 방법이 있다:
Pipe.SinkChannel sinkChannel = pipe.sink();
아래와 같이, SinkChannel의 write() 메소드를 호출하여 기록한다:
String newData = "New String to write to file..." + System.currentTimeMillis(); ByteBuffer buf = ByteBuffer.allocate(48); buf.clear(); buf.put(newData.getBytes()); buf.flip(); while(buf.hasRemaining()) { sinkChannel.write(buf); }
Reading from a Pipe
Pipe로부터 읽기 위해 소스 채널에 접근해야만 한다. 다음 수행 방법이 있다:
Pipe.SourceChannel sourceChannel = pipe.source();
아래와 같이, 소스 채널로부터 읽기 위해 read() 메소드를 호출한다:
ByteBuffer buf = ByteBuffer.allocate(48); int bytesRead = inChannel.read(buf);
read() 메소드에 의해 반환된 int는 버퍼에 얼마만큼의 bytes를 읽었는지 말해준다.