本文共 6334 字,大约阅读时间需要 21 分钟。
文件copy是java的io部分不可忽视的内容。 我是李福春,我在准备面试,今天的问题是: zero-copy是怎么回事? 操作系统的空间划分为内核态空间, 用户态空间; 内核态空间相对操作系统具备更高的权限和优先级; 用户态空间即普通用户所处空间。 zero-copy指的使用类似java.nio的transforTo方法进行文件copy,文件的copy直接从磁盘到内核态空间,不经过用户态空间,再写到磁盘,减少了io的消耗,避免了不必要的copy 和上下文切换,所以比较高效。 接下来对面试官可能扩展的问题进行一些拓展:package org.example.mianshi.filecopy;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.file.Files;/** * 说明:传统的文件copy * @author carter * 创建时间: 2020年03月26日 9:32 上午 **/public class JioFileCopyApp { public static void main(String[] args) { final File d = new File("/data/appenvs/denv.properties"); final File s = new File("/data/appenvs/env.properties"); System.out.println("source file content :" + s.exists()); System.out.println("target file content :" + d.exists()); System.out.println("source content:"); try { Files.lines(s.toPath()).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } System.out.println("do file copy !"); copy(s, d); System.out.println("target file content :" + d.exists()); System.out.println("target content:"); try { Files.lines(d.toPath()).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } private static void copy(File s, File d) { try ( final FileInputStream fileInputStream = new FileInputStream(s); final FileOutputStream fileOutputStream = new FileOutputStream(d) ) { byte[] buffer = new byte[1024]; int length; while ((length = fileInputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } }}代码可以运行;copy流程如下图:它不是zero-copy的,需要切换用户态空间和内核态空间,路径比较长,io消耗和上线文切换的消耗比较明显,这是比较低效的copy.
package org.example.mianshi.filecopy;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;import java.nio.file.Files;/** * 说明:传统的文件copy * @author carter * 创建时间: 2020年03月26日 9:32 上午 **/public class JnioFileCopyApp { public static void main(String[] args) { final File d = new File("/data/appenvs/ndenv.properties"); final File s = new File("/data/appenvs/env.properties"); System.out.println(s.getAbsolutePath() + "source file content :" + s.exists()); System.out.println(d.getAbsolutePath() +"target file content :" + d.exists()); System.out.println("source content:"); try { Files.lines(s.toPath()).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } System.out.println("do file copy !"); copy(s, d); System.out.println(d.getAbsolutePath() +"target file content :" + d.exists()); System.out.println("target content:"); try { Files.lines(d.toPath()).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } private static void copy(File s, File d) { try ( final FileChannel sourceFileChannel = new FileInputStream(s).getChannel(); final FileChannel targetFileChannel = new FileOutputStream(d).getChannel() ) { for (long count= sourceFileChannel.size();count>0;){ final long transferTo = sourceFileChannel.transferTo(sourceFileChannel.position(), count, targetFileChannel); count-=transferTo; } } catch (IOException e) { e.printStackTrace(); } }}
copy过程如下图:明显,不用经过用户态空间,是zero-copy,减少了io的消耗以及上下文切换,比较高效。
package org.example.mianshi.filecopy;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.file.CopyOption;import java.nio.file.Files;import java.nio.file.StandardCopyOption;/** * 说明:Files的文件copy * @author carter * 创建时间: 2020年03月26日 9:32 上午 **/public class FilesFileCopyApp { public static void main(String[] args) { final File d = new File("/data/appenvs/fenv.properties"); final File s = new File("/data/appenvs/env.properties"); System.out.println("source file content :" + s.exists()); System.out.println("target file content :" + d.exists()); System.out.println("source content:"); try { Files.lines(s.toPath()).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } System.out.println("do file copy !"); copy(s, d); System.out.println("target file content :" + d.exists()); System.out.println("target content:"); try { Files.lines(d.toPath()).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } private static void copy(File s, File d) { try { Files.copy(s.toPath(),d.toPath(), StandardCopyOption.COPY_ATTRIBUTES); } catch (IOException e) { e.printStackTrace(); } }}面试官一般喜欢刨根问底,那么来吧!贴一下源码:
public static Path copy(Path source, Path target, CopyOption... options) throws IOException { FileSystemProvider provider = provider(source); if (provider(target) == provider) { // same provider provider.copy(source, target, options); } else { // different providers CopyMoveHelper.copyToForeignTarget(source, target, options); } return target; }底层通过SPI,即ServiceLoader的方式加载不同文件系统的本地处理代码。 分类如下: 我们使用最多的UnixFsProvider,实际上是 直接从 用户态空间copy到用户态空间,使用了本地方法内联加持优化,但是它不是zero-copy, 性能也不会太差。
原创不易,转载请注明出处,让我们互通有无,共同进步,欢迎多沟通交流