`

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 IO与NIO:深入理解与实践指南

    Java IO和NIO提供了两种不同的I/O处理方式,各有优势和适用场景。IO适用于简单的I/O操作,而NIO则适合于需要高性能和高并发的应用。了解这两种I/O处理方式的区别和特点,可以帮助开发者根据具体的应用需求选择合适的...

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

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

    java nio ppt

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

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

    Servlet API和NIO(New IO)是Java编程中两个重要的概念,它们在处理网络I/O操作上有着不同的优势。Servlet API是Java服务器端编程的重要组成部分,主要用于构建动态Web应用程序,而NIO则是一种非阻塞的I/O模型,...

    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”文件,即可...

    Java NIO:浅析IO模型_动力节点Java学院整理

    Java NIO:浅析IO模型 Java NIO是Java语言中用于高性能I/O操作的API,理解IO模型是学习Java NIO的基础。本文将从同步和异步的概念开始,然后介绍阻塞和非阻塞的区别,接着介绍阻塞IO和非阻塞IO的区别,最后介绍五种...

    深入Java NIO:释放IO性能的新维度

    ### 深入Java NIO:释放IO性能的新维度 #### 一、Java NIO的革新特性 ##### 1. 非阻塞 I/O 操作 非阻塞I/O操作是NIO的一个核心特性,它与传统的阻塞式I/O相比,在性能上有了质的飞跃。在传统的阻塞式I/O模型中,当...

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

    全面理解 Java 网络编程 - BIO、NIO、AIO 本课程旨在帮助学生全面理解 Java 网络编程中的 BIO、NIO、AIO 三剑客,掌握 RPC 编程的基础知识,并结合实战项目巩固所学。 一、网络编程三剑客 - BIO、NIO、AIO BIO...

    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”文件,即可纵览...

    Java中的NIO:深入理解与IO的区别、原理及代码实现

    在Java编程中,IO(Input/Output)和NIO(New Input/Output)是处理数据流的两种不同机制。随着JDK 1.4的引入,NIO以其高效性和灵活性成为了并发编程中不可或缺的一部分。本文将深入探讨Java中的NIO,包括它与IO的...

    java NIO 视频教程

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

    nio:Clojure对java.nio的支持

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

    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”文件,即可纵览文档内容。 人性化翻译,文档中的...

    Using NIO to copy Java file fast.zip_java nio

    在"Using NIO to copy Java file fast"的例子中,开发者可能使用了以下步骤来快速复制文件: 1. 打开源文件和目标文件的FileChannel。 2. 创建一个ByteBuffer作为数据传输的中介。 3. 使用FileChannel的read()方法...

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

    【postgres-nio】是一个专为PostgreSQL数据库设计的非阻塞、事件驱动的Swift客户端库。这个库充分利用了SwiftNIO框架,使得开发者能够在构建高性能、低延迟的应用时,享受到异步I/O的优势。SwiftNIO是Apple为Swift...

    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 ...

    java NIO详细教程

    FileChannel fileChannel = new FileInputStream(file).getChannel(); // 创建一个ByteBuffer ByteBuffer buffer = ByteBuffer.allocate(1024); // 将数据从FileChannel读入buffer fileChannel.read(buffer); // ...

    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 客户端。 目前,该项目仍在进行中。 有关更新,请在我们的矩阵房→ 。 想试一试吗? 加入公共 。入门由于仁王使用斯威夫特软件包管理器,所有你需要做的是克隆的项目,在...

Global site tag (gtag.js) - Google Analytics