文件copy是java的io部分不可忽视的内容。
我是李福春,我在准备面试,今天的问题是:
zero-copy是怎么回事?
操做系统的空间划分为内核态空间, 用户态空间;
内核态空间相对操做系统具有更高的权限和优先级;
用户态空间即普通用户所处空间。
zero-copy指的使用相似java.nio的transforTo方法进行文件copy,文件的copy直接从磁盘到内核态空间,不通过用户态空间,再写到磁盘,减小了io的消耗,避免了没必要要的copy 和上下文切换,因此比较高效。
接下来对面试官可能扩展的问题进行一些拓展:
java
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(); } } }
面试官通常喜欢刨根问底,那么来吧!贴一下源码:
app
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的方式加载不一样文件系统的本地处理代码。
分类以下:
ide
咱们使用最多的UnixFsProvider,其实是 直接从 用户态空间copy到用户态空间,使用了本地方法内联加持优化,可是它不是zero-copy, 性能也不会太差。
工具
1, 使用缓存,减小io的操做次数;
2,使用zero-copy,即相似 java.nio的 transferTo方法进行copy;
3, 减小传输过程当中没必要要的转换,好比编解码,最好直接二进制传输;
性能
buffer的类层级图以下:优化
除了bool其余7个原生类型都有对应的Buffer;
面试官若是问细节,先说4个属性, capacity, limit ,position, mark
再描述byteBuffer的读写流程。
而后是DirectBuffer,这个是直接操做堆外内存,比较高效。可是用比如较困难,除非是流媒体的行业,不会问的这么细,直接翻源码好好准备,问通常也是技术专家来问你了。
spa
本篇回答了什么是zero-copy,而后介绍了java体系实现文件copy的3中方式,(扩展的第三方库不算在内);
而后简要介绍了如何提升io效率的三种方法,以及提升内存利用率的Buffer作了系统级的介绍。
不啰嗦,能够快速经过下图条理化本篇内容,但愿对你有所帮助。
操作系统
原创不易,转载请注明出处,让咱们互通有无,共同进步,欢迎沟通交流。