In Java NIO you can transfer data directly from one channel to another, if one of the channels is a FileChannel. The FileChannel class has a transferTo() and a transferFrom()  method which does this for you.

transferFrom()

The FileChannel.transferFrom() method transfers data from a source channel into the FileChannel. Here is a simple example:

RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel toChannel = toFile.getChannel(); long position = 0;
long count = fromChannel.size(); toChannel.transferFrom(fromChannel, position, count);

The parameters position and count, tell where in the destination file to start writing (position), and how many bytes to transfer maximally (count). If the source channel has fewer than count bytes, less is transfered.

Additionally, some SocketChannel implementations may transfer only the data the SocketChannel has ready in its internal buffer here and now - even if the SocketChannel  may later have more data available. Thus, it may not transfer the entire data requested (count) from the SocketChannel into FileChannel.

transferTo()

The transferTo()  method transfer from a FileChannel into some other channel. Here is a simple example:

RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel toChannel = toFile.getChannel(); long position = 0;
long count = fromChannel.size(); fromChannel.transferTo(position, count, toChannel);

Notice how similar the example is to the previous. The only real difference is the which FileChannel object the method is called on. The rest is the same.

The issue with SocketChannel is also present with the transferTo() method. The SocketChannel implementation may only transfer bytes from the FileChannel until the send buffer is full, and then stop.

Ref:

http://tutorials.jenkov.com/java-nio/channel-to-channel-transfers.html

Java NIO Channel to Channel Transfers的更多相关文章

  1. 5. 彤哥说netty系列之Java NIO核心组件之Channel

    你好,我是彤哥,本篇是netty系列的第五篇. 简介 上一章我们一起学习了如何使用Java原生NIO实现群聊系统,这章我们一起来看看Java NIO的核心组件之一--Channel. 思维转变 首先, ...

  2. Java NIO 之 Socket Channel

    在Java NIO中用Channel来对程序与进行I/O操作主体的连接关系进行抽象,这些IO主体包括如文件.Socket或其他设备.简而言之,指代了一种与IO操作对象间的连接关系. 按照Channel ...

  3. Java NIO学习笔记---Channel

    Java NIO 的核心组成部分: 1.Channels 2.Buffers 3.Selectors 我们首先来学习Channels(java.nio.channels): 通道 1)通道基础 通道( ...

  4. Java NIO中的Channel接口

    1. Channel  通道,可以将指定文件的部分或全部直接映射成Buffer. 不能直接读写Channel中的数据,Channel只能与ByteBuffer交互. 读数据时,把Channel中的数据 ...

  5. Java NIO之通道Channel

    channel与流的区别: 流基于字节,且读写为单向的. 通道基于快Buffer,可以异步读写.除了FileChannel之外都是双向的. channel的主要实现: FileChannel Data ...

  6. JAVA NIO 之Channel

    缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.Channel 通道就是将数据传输给 ByteBuffer 对象或者从 ByteBuffer 对象获取数据进行传输. Channel 用于在 ...

  7. Java NIO学习系列二:Channel

    上文总结了Java NIO中的Buffer相关知识点,本文中我们来总结一下它的好兄弟:Channel.上文有说到,Java NIO中的Buffer一般和Channel配对使用,NIO中的所有IO都起始 ...

  8. Java NIO Channel 使用

    Java NIO 中的 Channel 分类: FileChannel SocketChannel ServerSocketChannel DatagramChannel channel 分类 Fil ...

  9. NIO之通道(Channel)的原理与获取以及数据传输与内存映射文件

    通道(Channel) 由java.nio.channels包定义的,Channel表示IO源与目标打开的连接,Channel类似于传统的“流”,只不过Channel本身不能直接访问数据,Channe ...

  10. [翻译]java nio 概述

    原文地址:http://tutorials.jenkov.com/java-nio/overview.html java NIO 包含一下核心内容: Channels Buffers Selector ...

随机推荐

  1. ubuntun 18.04 desktop安装jupyter-notebook

    在ubuntu18.04要安装jupyter-notebook,当然前提是先安装python,然后按如下步骤安装jupyter-notebook,现在记录如下: 1.sudo apt-get upda ...

  2. RegExp.$1

    在学习vue2的compile的模板解析的时候,会出现这个正则表达式,不是很清楚,所有就弄明白下并记录下来. RegExp 是javascript中的一个内置对象.为正则表达式.RegExp.$1是R ...

  3. linux学习笔记-13.进程控制

    1.查看用户最近登录情况 lastlastlog 2.查看硬盘使用情况 df 3.查看文件大小 du 4.查看内存使用情况 free 5.查看文件系统 /proc 6.查看日志 ls /var/log ...

  4. linux学习笔记-5.用户和组

    1.添加一个tom用户,设置它属于users组,并添加注释信息 分步完成: useradd tom usermod -g users tom usermod -c "hr tom" ...

  5. Spring重复扫描导致事务失败的解决方案及深入分析

    问题及日志使用Spring和mybatis,然后配置事务,出现SqlSession was not registered for synchronization because synchroniza ...

  6. http://blog.mn886.net/jqGrid/

    http://blog.mn886.net/jqGrid/  中文版 http://www.trirand.com/blog/jqgrid/jqgrid.html  英文版

  7. POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  8. HDU 5813 Elegant Construction 构造

    Elegant Construction 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  9. 使用 IntraWeb (17) - 基本控件之 TIWRadioButton、TIWRadioGroup、TIWCheckBox

    TIWRadioButton //单选 TIWRadioGroup //单选组 TIWCheckBox //复选 TIWRadioButton 所在单元及继承链: IWCompRadioButton. ...

  10. oracle定时任务的编写及查看删除

    declare jobno number; begin dbms_job.submit( jobno,--定时器ID,系统自动获得 'PRC_INSERT;', --what执行的过程名 sysdat ...