`

NIO: High Performance File Copying

阅读更多
In a previous tip, I discussed a simple file copy algorithm in context to the best way to move a directory of files (see IO: Moving a Directory ). The algorithm I posted was something of this sort:

public static void copyFile(File source, File dest) throws IOException {
 if(!dest.exists()) {
  dest.createNewFile();
 }
 InputStream in = null;
 OutputStream out = null;
 try {
  in = new new FileInputStream(source);
  out = new FileOutputStream(dest);
    
  // Transfer bytes from in to out
  byte[] buf = new byte[1024];
  int len;
  while ((len = in.read(buf)) > 0) {
   out.write(buf, 0, len);
  }
 }
 finally {
  if(in != null) {
   in.close();
  }
  if(out != null) {
   out.close();
  }
 }
}
 



One of the things to note in this algorithm is the verbosity and explicitness of the code. The code specifically defines a byte[] buffer, and sets the size to 1 kilobyte, and then it simply does kilobyte-at-a-time copies. The first potential problem with this is that the amount of optimal buffering isn't neccessarily 1 kilobyte. In addition to that, for this code to work, Java IO code must read data from the file system, bring it up into JVM memory, and then push it back down to the filesystem through Java IO.

We all remember when Java 1.4 came out that it brought the java.nio package with it - most of us also remember how that was pretty much it - after that there wasn't a whole lot of noise regarding 'NIO'. It's really a shame - Java NIO has the potential to really improve performance in a lot of areas. File copies is just one of them. Here is the basic file-to-file copy algorithm re-implemented using 'NIO':

public static void copyFile(File sourceFile, File destFile) throws IOException {
 if(!destFile.exists()) {
  destFile.createNewFile();
 }
 
 FileChannel source = null;
 FileChannel destination = null;
 try {
  source = new FileInputStream(sourceFile).getChannel();
  destination = new FileOutputStream(destFile).getChannel();
  destination.transferFrom(source, 0, source.size());
 }
 finally {
  if(source != null) {
   source.close();
  }
  if(destination != null) {
   destination.close();
  }
}



The first thing you'll notice about this implementation is the difference in core copying logic:

  byte[] buf = new byte[1024];
  int len;
  while ((len = in.read(buf)) > 0) {
   out.write(buf, 0, len);
  }



... becomes:

 destination.transferFrom(source, 0, source.size());



Note that there is no reference to the buffering used or the implementation of the actual copy algorithm. This is key to the potential performance advantages of this algorithm. The 'transferFrom' algorithm has the advantage of being able to be optimized to a much higher level than most of us would want to try. For one thing, because of the design of the interacting objects, chances are good that on most platforms the copy request can be deferred directly to the underlying operating system. Let it be known that in most cases the OS will be faster at copying files than Java. Just the same, even if it can't actually defer directly to the OS for the copy, because the transferFrom algorithm is related to the underlying channel implementation, it can be optimized for the platform, context, and channel type, it can use native method calls, and do many other fancy things. Long story short, the transferFrom algorithm can be optimized and optimized and optimized (and is ).

Just to verify, I filled a folder with a ton of small and large files, just to see what would happen. It seems, on average, that there is about a 33% improvement in performance (yes, 1/3rd !!!) from the rather simple copy algorithm above. Not too shabby!

 

http://www.javalobby.org/java/forums/t17036.html

分享到:
评论

相关推荐

    一站式学习Java网络编程 全面理解BIO:NIO:AIO1

    一站式学习Java网络编程 全面理解BIO:NIO:AIO1

    java nio ppt

    java nio ppt java.nio: High Performance I/O for Java

    httpcore-nio-4.4.6-API文档-中英对照版.zip

    Maven坐标:org.apache.httpcomponents:httpcore-nio:4.4.6; 标签:apache、httpcomponents、nio、httpcore、jar包、java、中英对照文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可...

    httpcore-nio-4.4.14-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpcore-nio:4.4.14; 标签:apache、httpcomponents、httpcore、nio、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览...

    Servlet API 和 NIO: 最终组合在一起

    NULL 博文链接:https://conkeyn.iteye.com/blog/523234

    java NIO 视频教程

    Java NIO: Channels and Buffers(通道和缓冲区) 标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(Buffer)进行操作,数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中。 ...

    postgres-nio::elephant:用于PostgreSQL非阻塞,事件驱动的Swift客户端

    postgres-nio::elephant:用于PostgreSQL非阻塞,事件驱动的Swift客户端

    java nio教程pdf

    Java NIO: Channels and Buffers(通道和缓冲区) 标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(Buffer)进行操作,数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中。 ...

    nio:用于Nim语言的迷你服务器框架

    nio:用于Nim语言的迷你服务器框架

    nio:Clojure对java.nio的支持

    o Clojure对java.nio的支持。 将clojure.java.io的输入流,输出流和复制功能扩展到java.nio类。 定义新的强制功能缓冲区,字节缓冲区,字符缓冲区,双缓冲区,浮点缓冲区,整数缓冲区,长缓冲区,短缓冲区,通道,可...

    Using NIO to copy Java file fast.zip_java nio

    Using NIO to copy Java file fast

    xnio-nio-3.8.4.Final-API文档-中文版.zip

    Maven坐标:org.jboss.xnio:xnio-nio:3.8.4.Final; 标签:jboss、xnio、nio、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的...

    优雅的操作文件:java.nio.file 库介绍.pdf

    但 Java 在后期版本中引入了 java.nio.file 库来提高 Java 对文件操作的能力。还增加的流的功能,似乎使得文件变成更好用了。所以本章,我们就来主要介绍 java.nio.file 中常用的类和模块,大致如下: Path 路径:...

    mqtt-nio:Swift NIO MQTT 3.1.1客户端

    MQTT NIO 一个基于Swift NIO的MQTT 3.1.1客户端,通过NIOSSL和NIOTransportServices支持NIOTransportServices(iOS必需),WebSocket连接和TLS。 MQTT(消息队列遥测传输)是IBM开发的轻量级消息协议,于1999年...

    java8源码-nio:java8nio使用的总结

    nio java8 nio使用的总结 目录 1. NIO_NIO 与 IO 区别 NIO支持面向缓冲区的、基于通道的IO操作 IO NIO 面向流(Stream Oriented) 面向缓冲区(Buffer Oriented) 阻塞IO(Blocking IO) 非阻塞IO(NonBlocking ...

    nio:Go #golang中的并发缓冲IO

    ReadWriter} nio的Copy方法同时从io.Reader复制到提供的nio.Buffer,然后从nio.Buffer复制到io.Writer。 这样,阻止写入不会降低io.Reader的速度。 import ( "github....

    nio:o Nio是即将推出的iOS矩阵客户端

    :speech_balloon: o Nio是即将推出的iOS 客户端。 目前,该项目仍在进行中。 有关更新,请在我们的矩阵房→ 。 想试一试吗? 加入公共 。入门由于仁王使用斯威夫特软件包管理器,所有你需要做的是克隆的项目,在...

    yubo-java-nio:java nio的学习项目

    yubo-java-nioNIO 直接缓冲区 VS 非直接缓冲区直接缓冲区1、直接缓冲区最适合I/O 2、创建成本比非直接缓冲区高 3、直接缓冲区使用的内存是通过调用原生的、操作系统特定的代码来分配的 4、内存存储区域不受限制垃圾...

    zio-nio:与NIO相连的小型ZIO接口

    ZIO-NIO CI 释放快照不和谐 Java NIO的ZIO接口。 Java NIO是不安全的,例如,隐藏IO操作中的实际错误并在IO成功/不成功时仅给您提供true / false值,这可能会让您大吃一惊。 另一方面,ZIO-NIO包含ZIO效果,环境,...

    Java NIO:浅析I/O模型

    也许很多朋友在学习NIO的时候都会感觉有点吃力,对里面的很多概念都感觉不是那么明朗。在进入Java NIO编程之前,我们先来讨论一些比较基础的知识:I/O模型。下面本文先从同步和异步的概念 说起,然后接着阐述了阻塞...

Global site tag (gtag.js) - Google Analytics