NIOFileChannel零拷贝复制文件

编程

下面这个会先拷贝到用户空间

public void test2() throws IOException { // 220

long start = System.currentTimeMillis();

try (

FileInputStream inputStream = new FileInputStream("rop.mp4");

FileChannel inChannel = inputStream.getChannel();

FileOutputStream outputStream = new FileOutputStream("rop2.mp4");

FileChannel outChannel = outputStream.getChannel();

) {

ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

while (inChannel.read(byteBuffer) != -1) {

byteBuffer.flip();

outChannel.write(byteBuffer);

byteBuffer.clear();

}

}

System.out.println(System.currentTimeMillis() - start);

}

下面这个是零拷贝

public void test5() throws IOException { // 29

long start = System.currentTimeMillis();

try (

FileChannel inChannel = FileChannel.open(Paths.get("rop.mp4"), StandardOpenOption.READ);

FileChannel outChannel = FileChannel.open(Paths.get("rop2.mp4"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE)

) {

//inChannel.transferTo(0, inChannel.size(), outChannel);

outChannel.transferFrom(inChannel, 0, inChannel.size());

}

System.out.println(System.currentTimeMillis() - start);

}

以上是 NIOFileChannel零拷贝复制文件 的全部内容, 来源链接: utcz.com/z/516187.html

回到顶部