ByteBuffer
을 사용하여 채널에 대한 I/O 작업을 수행하려고합니다.ByteBuffer를 사용하는 채널의 I/O 연산 - 성능 비교
FileChannel inChannel = new FileInputStream("input.txt").getChannel();
FileChannel outChannel = new FileOutputStream("output.txt").getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
방법 1 : compact()
while (inChannel.read(buf) != -1 || buf.position() > 0) {
buf.flip();
outChannel.write(buf);
buf.compact();
}
방법 3을 사용 : hasRemaining()
while (inChannel.read(buf) != -1) {
buf.flip();
while (buf.hasRemaining()) {
outChannel.write(buf);
}
buf.clear();
}
방법 2를 사용하여 마지막으로 나는 세 가지 솔루션을 함께했다 하이브리드 모델
while (inChannel.read(buf) != -1) {
buf.flip();
outChannel.write(buf);
buf.compact();
}
// final flush of pending output
while (buf.hasRemaining())
outChannel.write(buf);
질문 : 가장 성능과 처리량이 높은 방법은 무엇입니까?
이것은 내가 알고 싶은 것입니다. 이것에 대한 결론이 있습니까? 주어진 답변 외에? http://vanillajava.blogspot.kr/2011/11/stupid-performance-trick-in-java.html –